Skip to contentSkip to conversation

Update opentitan uart (part of supporting opentitan version 1)

PATCH Superseded ~lexbaileylowrisc 5 participants 9 messages Mar 19 – Apr 1

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Update opentitan uart (part of supporting opentitan version 1)

This commit is part of our efforts to get support for opentitan version 1 in qemu, it replaces the previous proposed change, and incorporates some requests from review comments.

The changes are now being made in the existing opentitan machine, instead of in a new machine. We have also avoided adding any new helper units.

This updates the UART device to match our branch, and to match version 1 of opentitan.
This is not where we were going to start previously, but now this is the easiest starting point.

This commit adds a file that is a substantial copy of code already published under the MIT licence, and we aren't able to relicence it as GPL. (unable to get unanimous author agreement)

Lex Bailey (1):
Replace opentitan UART device with newer version (opentitan 1.0)

 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
Signature · 1 line
2.49.1
#1 lore raw

PATCH 1/1 Replace opentitan UART device with newer version (opentitan 1.0)

From: Lex Bailey <[email protected]>

---
 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
hw/char/ibex_uart.c deleted +0 -569
deleted file mode 100644
index 127d219df3..0000000000
@@ -1,569 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * For details check the documentation here:
- *    https://docs.opentitan.org/hw/ip/uart/doc/
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "qemu/osdep.h"
-#include "hw/char/ibex_uart.h"
-#include "hw/core/irq.h"
-#include "hw/core/qdev-clock.h"
-#include "hw/core/qdev-properties.h"
-#include "hw/core/qdev-properties-system.h"
-#include "hw/core/registerfields.h"
-#include "migration/vmstate.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
-
-REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
-REG32(INTR_ENABLE, 0x04)
-REG32(INTR_TEST, 0x08)
-REG32(ALERT_TEST, 0x0C)
-REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
-    FIELD(CTRL, NF, 2, 1)
-    FIELD(CTRL, SLPBK, 4, 1)
-    FIELD(CTRL, LLPBK, 5, 1)
-    FIELD(CTRL, PARITY_EN, 6, 1)
-    FIELD(CTRL, PARITY_ODD, 7, 1)
-    FIELD(CTRL, RXBLVL, 8, 2)
-    FIELD(CTRL, NCO, 16, 16)
-REG32(STATUS, 0x14)
-    FIELD(STATUS, TXFULL, 0, 1)
-    FIELD(STATUS, RXFULL, 1, 1)
-    FIELD(STATUS, TXEMPTY, 2, 1)
-    FIELD(STATUS, RXIDLE, 4, 1)
-    FIELD(STATUS, RXEMPTY, 5, 1)
-REG32(RDATA, 0x18)
-REG32(WDATA, 0x1C)
-REG32(FIFO_CTRL, 0x20)
-    FIELD(FIFO_CTRL, RXRST, 0, 1)
-    FIELD(FIFO_CTRL, TXRST, 1, 1)
-    FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
-REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
-REG32(OVRD, 0x28)
-REG32(VAL, 0x2C)
-REG32(TIMEOUT_CTRL, 0x30)
-
-static void ibex_uart_update_irqs(IbexUartState *s)
-{
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
-    }
-}
-
-static int ibex_uart_can_receive(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
-    }
-
-    return 0;
-}
-
-static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
-{
-    IbexUartState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
-
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
-
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-}
-
-static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
-                               void *opaque)
-{
-    IbexUartState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
-    }
-
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
-    }
-
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
-
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
-
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ibex_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
-    }
-
-    /* Clear the TX Full bit */
-    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-    }
-
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-    }
-
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
-}
-
-static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
-                               int size)
-{
-    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
-        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
-    }
-
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
-
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
-    }
-
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
-    }
-
-    timer_mod(s->fifo_trigger_handle, current_time +
-              (s->char_tx_time * 4));
-}
-
-static void ibex_uart_reset(DeviceState *dev)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
-
-    s->tx_level = 0;
-    s->rx_level = 0;
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
-
-    ibex_uart_update_irqs(s);
-}
-
-static uint64_t ibex_uart_get_baud(IbexUartState *s)
-{
-    uint64_t baud;
-
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
-    baud *= clock_get_hz(s->f_clk);
-    baud >>= 20;
-
-    return baud;
-}
-
-static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
-                                       unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint64_t retvalue = 0;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
-        break;
-    case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_CTRL:
-        retvalue = s->uart_ctrl;
-        break;
-    case R_STATUS:
-        retvalue = s->uart_status;
-        break;
-
-    case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
-        break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
-        break;
-    case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
-        break;
-
-    case R_OVRD:
-        retvalue = s->uart_ovrd;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        retvalue = s->uart_val;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-        return 0;
-    }
-
-    return retvalue;
-}
-
-static void ibex_uart_write(void *opaque, hwaddr addr,
-                                  uint64_t val64, unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint32_t value = val64;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        /* Write 1 clear */
-        s->uart_intr_state &= ~value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_TEST:
-        s->uart_intr_state |= value;
-        ibex_uart_update_irqs(s);
-        break;
-
-    case R_CTRL:
-        s->uart_ctrl = value;
-
-        if (value & R_CTRL_NF_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_NF is not supported\n", __func__);
-        }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_LLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_LLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_PARITY_EN_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_EN is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_PARITY_ODD_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_RXBLVL_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
-        }
-        if (value & R_CTRL_NCO_MASK) {
-            uint64_t baud = ibex_uart_get_baud(s);
-
-            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-        }
-        break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
-    case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
-        break;
-
-    case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
-
-        if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
-        }
-        if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
-        }
-        break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
-
-    case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-    }
-}
-
-static void ibex_uart_clk_update(void *opaque, ClockEvent event)
-{
-    IbexUartState *s = opaque;
-
-    /* recompute uart's speed on clock change */
-    uint64_t baud = ibex_uart_get_baud(s);
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-}
-
-static void fifo_trigger_update(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ibex_uart_xmit(NULL, G_IO_OUT, s);
-    }
-}
-
-static const MemoryRegionOps ibex_uart_ops = {
-    .read = ibex_uart_read,
-    .write = ibex_uart_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
-    .impl.min_access_size = 4,
-    .impl.max_access_size = 4,
-};
-
-static int ibex_uart_post_load(void *opaque, int version_id)
-{
-    IbexUartState *s = opaque;
-
-    ibex_uart_update_irqs(s);
-    return 0;
-}
-
-static const VMStateDescription vmstate_ibex_uart = {
-    .name = TYPE_IBEX_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .post_load = ibex_uart_post_load,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
-                            IBEX_UART_TX_FIFO_SIZE),
-        VMSTATE_UINT32(tx_level, IbexUartState),
-        VMSTATE_UINT64(char_tx_time, IbexUartState),
-        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
-        VMSTATE_UINT32(uart_intr_state, IbexUartState),
-        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
-        VMSTATE_UINT32(uart_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_status, IbexUartState),
-        VMSTATE_UINT32(uart_rdata, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
-        VMSTATE_UINT32(uart_ovrd, IbexUartState),
-        VMSTATE_UINT32(uart_val, IbexUartState),
-        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const Property ibex_uart_properties[] = {
-    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
-};
-
-static void ibex_uart_init(Object *obj)
-{
-    IbexUartState *s = IBEX_UART(obj);
-
-    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
-                                  ibex_uart_clk_update, s, ClockUpdate);
-    clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
-
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
-
-    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
-                          TYPE_IBEX_UART, 0x400);
-    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
-}
-
-static void ibex_uart_realize(DeviceState *dev, Error **errp)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                          fifo_trigger_update, s);
-
-    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
-                             ibex_uart_receive, NULL, NULL,
-                             s, NULL, true);
-}
-
-static void ibex_uart_class_init(ObjectClass *klass, const void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-
-    device_class_set_legacy_reset(dc, ibex_uart_reset);
-    dc->realize = ibex_uart_realize;
-    dc->vmsd = &vmstate_ibex_uart;
-    device_class_set_props(dc, ibex_uart_properties);
-    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
-}
-
-static const TypeInfo ibex_uart_info = {
-    .name          = TYPE_IBEX_UART,
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(IbexUartState),
-    .instance_init = ibex_uart_init,
-    .class_init    = ibex_uart_class_init,
-};
-
-static void ibex_uart_register_types(void)
-{
-    type_register_static(&ibex_uart_info);
-}
-
-type_init(ibex_uart_register_types)
hw/char/meson.build +1 -1
index fc3d7ee506..989ca3d7e0 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))
 system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true: files('cmsdk-apb-uart.c'))
 system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
 system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_IBEX', if_true: files('ot_uart.c'))
 system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
 system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true: files('ipoctal232.c'))
 system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c new file +635 -0

635 lines not on this page · raw message

hw/char/trace-events +8 -0
index a3fcc77287..c859d8af4e 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned pclk, unsigned baud) "%s: @ %u Hz: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +2 -1
index 309125e854..163d3ac3d3 100644
@@ -132,7 +132,8 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ibex_uart.h deleted +0 -73

73 lines not on this page · raw message

include/hw/char/ot_uart.h new file +133 -0

133 lines not on this page · raw message

include/hw/riscv/opentitan.h +2 -2
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
 #include "hw/timer/ibex_timer.h"
 #include "hw/ssi/ibex_spi_host.h"
 #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
     IbexTimerState timer;
     IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
Signature · 1 line
2.49.1
#2 lore raw

Re: [PATCH qemu 1/1] Replace opentitan UART device with newer version (opentitan 1.0)

Quoted 1 line · ~lexbaileylowrisc

You need a commit message, it should describe what you are doing, link to specs and provide details for future readers.

hw/char/ibex_uart.c

Quoted 23 lines

From: Lex Bailey <[email protected]>

---
 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
hw/char/ibex_uart.c deleted +0 -0
deleted file mode 100644
index 127d219df3..0000000000

I don't think we should remove the file. If you want to rename it that's fine, but that should be its own commit. So one commit to rename it (with a justification in the commit message) and then more commits to make changes.

Quoted 611 lines
hw/char/ibex_uart.c deleted +0 -569
@@ -1,569 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * For details check the documentation here:
- *    https://docs.opentitan.org/hw/ip/uart/doc/
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "qemu/osdep.h"
-#include "hw/char/ibex_uart.h"
-#include "hw/core/irq.h"
-#include "hw/core/qdev-clock.h"
-#include "hw/core/qdev-properties.h"
-#include "hw/core/qdev-properties-system.h"
-#include "hw/core/registerfields.h"
-#include "migration/vmstate.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
-
-REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
-REG32(INTR_ENABLE, 0x04)
-REG32(INTR_TEST, 0x08)
-REG32(ALERT_TEST, 0x0C)
-REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
-    FIELD(CTRL, NF, 2, 1)
-    FIELD(CTRL, SLPBK, 4, 1)
-    FIELD(CTRL, LLPBK, 5, 1)
-    FIELD(CTRL, PARITY_EN, 6, 1)
-    FIELD(CTRL, PARITY_ODD, 7, 1)
-    FIELD(CTRL, RXBLVL, 8, 2)
-    FIELD(CTRL, NCO, 16, 16)
-REG32(STATUS, 0x14)
-    FIELD(STATUS, TXFULL, 0, 1)
-    FIELD(STATUS, RXFULL, 1, 1)
-    FIELD(STATUS, TXEMPTY, 2, 1)
-    FIELD(STATUS, RXIDLE, 4, 1)
-    FIELD(STATUS, RXEMPTY, 5, 1)
-REG32(RDATA, 0x18)
-REG32(WDATA, 0x1C)
-REG32(FIFO_CTRL, 0x20)
-    FIELD(FIFO_CTRL, RXRST, 0, 1)
-    FIELD(FIFO_CTRL, TXRST, 1, 1)
-    FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
-REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
-REG32(OVRD, 0x28)
-REG32(VAL, 0x2C)
-REG32(TIMEOUT_CTRL, 0x30)
-
-static void ibex_uart_update_irqs(IbexUartState *s)
-{
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
-    }
-}
-
-static int ibex_uart_can_receive(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
-    }
-
-    return 0;
-}
-
-static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
-{
-    IbexUartState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
-
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
-
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-}
-
-static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
-                               void *opaque)
-{
-    IbexUartState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
-    }
-
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
-    }
-
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
-
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
-
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ibex_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
-    }
-
-    /* Clear the TX Full bit */
-    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-    }
-
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-    }
-
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
-}
-
-static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
-                               int size)
-{
-    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
-        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
-    }
-
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
-
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
-    }
-
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
-    }
-
-    timer_mod(s->fifo_trigger_handle, current_time +
-              (s->char_tx_time * 4));
-}
-
-static void ibex_uart_reset(DeviceState *dev)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
-
-    s->tx_level = 0;
-    s->rx_level = 0;
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
-
-    ibex_uart_update_irqs(s);
-}
-
-static uint64_t ibex_uart_get_baud(IbexUartState *s)
-{
-    uint64_t baud;
-
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
-    baud *= clock_get_hz(s->f_clk);
-    baud >>= 20;
-
-    return baud;
-}
-
-static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
-                                       unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint64_t retvalue = 0;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
-        break;
-    case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_CTRL:
-        retvalue = s->uart_ctrl;
-        break;
-    case R_STATUS:
-        retvalue = s->uart_status;
-        break;
-
-    case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
-        break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
-        break;
-    case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
-        break;
-
-    case R_OVRD:
-        retvalue = s->uart_ovrd;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        retvalue = s->uart_val;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-        return 0;
-    }
-
-    return retvalue;
-}
-
-static void ibex_uart_write(void *opaque, hwaddr addr,
-                                  uint64_t val64, unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint32_t value = val64;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        /* Write 1 clear */
-        s->uart_intr_state &= ~value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_TEST:
-        s->uart_intr_state |= value;
-        ibex_uart_update_irqs(s);
-        break;
-
-    case R_CTRL:
-        s->uart_ctrl = value;
-
-        if (value & R_CTRL_NF_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_NF is not supported\n", __func__);
-        }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_LLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_LLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_PARITY_EN_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_EN is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_PARITY_ODD_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_RXBLVL_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
-        }
-        if (value & R_CTRL_NCO_MASK) {
-            uint64_t baud = ibex_uart_get_baud(s);
-
-            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-        }
-        break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
-    case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
-        break;
-
-    case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
-
-        if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
-        }
-        if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
-        }
-        break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
-
-    case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-    }
-}
-
-static void ibex_uart_clk_update(void *opaque, ClockEvent event)
-{
-    IbexUartState *s = opaque;
-
-    /* recompute uart's speed on clock change */
-    uint64_t baud = ibex_uart_get_baud(s);
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-}
-
-static void fifo_trigger_update(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ibex_uart_xmit(NULL, G_IO_OUT, s);
-    }
-}
-
-static const MemoryRegionOps ibex_uart_ops = {
-    .read = ibex_uart_read,
-    .write = ibex_uart_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
-    .impl.min_access_size = 4,
-    .impl.max_access_size = 4,
-};
-
-static int ibex_uart_post_load(void *opaque, int version_id)
-{
-    IbexUartState *s = opaque;
-
-    ibex_uart_update_irqs(s);
-    return 0;
-}
-
-static const VMStateDescription vmstate_ibex_uart = {
-    .name = TYPE_IBEX_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .post_load = ibex_uart_post_load,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
-                            IBEX_UART_TX_FIFO_SIZE),
-        VMSTATE_UINT32(tx_level, IbexUartState),
-        VMSTATE_UINT64(char_tx_time, IbexUartState),
-        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
-        VMSTATE_UINT32(uart_intr_state, IbexUartState),
-        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
-        VMSTATE_UINT32(uart_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_status, IbexUartState),
-        VMSTATE_UINT32(uart_rdata, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
-        VMSTATE_UINT32(uart_ovrd, IbexUartState),
-        VMSTATE_UINT32(uart_val, IbexUartState),
-        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const Property ibex_uart_properties[] = {
-    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
-};
-
-static void ibex_uart_init(Object *obj)
-{
-    IbexUartState *s = IBEX_UART(obj);
-
-    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
-                                  ibex_uart_clk_update, s, ClockUpdate);
-    clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
-
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
-
-    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
-                          TYPE_IBEX_UART, 0x400);
-    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
-}
-
-static void ibex_uart_realize(DeviceState *dev, Error **errp)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                          fifo_trigger_update, s);
-
-    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
-                             ibex_uart_receive, NULL, NULL,
-                             s, NULL, true);
-}
-
-static void ibex_uart_class_init(ObjectClass *klass, const void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-
-    device_class_set_legacy_reset(dc, ibex_uart_reset);
-    dc->realize = ibex_uart_realize;
-    dc->vmsd = &vmstate_ibex_uart;
-    device_class_set_props(dc, ibex_uart_properties);
-    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
-}
-
-static const TypeInfo ibex_uart_info = {
-    .name          = TYPE_IBEX_UART,
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(IbexUartState),
-    .instance_init = ibex_uart_init,
-    .class_init    = ibex_uart_class_init,
-};
-
-static void ibex_uart_register_types(void)
-{
-    type_register_static(&ibex_uart_info);
-}
-
-type_init(ibex_uart_register_types)
hw/char/meson.build +1 -1
index fc3d7ee506..989ca3d7e0 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))
 system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true: files('cmsdk-apb-uart.c'))
 system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
 system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_IBEX', if_true: files('ot_uart.c'))
 system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
 system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true: files('ipoctal232.c'))
 system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c new file +22 -0
new file mode 100644
index 0000000000..757f64ab70
@@ -0,0 +1,635 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025 lowRISC contributors.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Based on original ibex_uart implementation:
+ *  Copyright (c) 2020 Western Digital
+ *  Alistair Francis <[email protected]>
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/fifo8.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qapi/error.h"

hw/char/ot_uart.c line 32

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+#include "chardev/char-fe.h"
+#include "hw/core/cpu.h"
+#include "hw/core/irq.h"
+#include "hw/char/ot_uart.h"
+#include "hw/core/qdev-properties-system.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/registerfields.h"
+#include "trace.h"
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)

Let's use the existing QEMU macros instead of creating custom ones.

Quoted 13 lines
hw/char/ot_uart.c new file +13 -0
+static const char *REG_NAMES[REGS_COUNT] = {
+    /* clang-format off */
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),

hw/char/ot_uart.c line 55

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+    /* clang-format on */
+};
+#undef REG_NAME_ENTRY
+
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_TXILVL_MASK) >>
+                       R_FIFO_CTRL_TXILVL_SHIFT;

You can then use the extract (FIELD_EX32) macros instead of mask and shift.

hw/char/ot_uart.c line 57

Quoted 2 lines
hw/char/ot_uart.c new file +2 -0
+
+    return tx_ilvl < 7u ? (1u << tx_ilvl) : 64u;

You shouldn't need the us

Quoted 16 lines
hw/char/ot_uart.c new file +16 -0
+}
+
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_RXILVL_MASK) >>
+                       R_FIFO_CTRL_RXILVL_SHIFT;
+
+    return rx_ilvl < 7u ? (1u << rx_ilvl) : 126u;
+}
+
+static void ot_uart_update_irqs(OtUARTState *s)
+{
+    uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
+
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE],
+                       state_masked);

hw/char/ot_uart.c line 83

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+
+    for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
+        bool level = (state_masked & (1U << index)) != 0;
+        qemu_set_irq(s->irqs[index], level);
+    }
+}
+
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);

You also should need to cast this

Quoted 222 lines
hw/char/ot_uart.c new file +222 -0
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
+static void ot_uart_check_baudrate(const OtUARTState *s)
+{
+    uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO);
+
+    unsigned baudrate = (unsigned)(((uint64_t)nco * (uint64_t)s->pclk) >>
+                                   (R_CTRL_NCO_LENGTH + 4));
+
+    if (baudrate) {
+        trace_ot_uart_check_baudrate(s->ot_id, s->pclk, baudrate);
+    }
+}
+
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+}
+
+static int ot_uart_can_receive(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
+    }
+
+    return 0;
+}
+
+static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    OtUARTState *s = opaque;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
+
+    if (size && !s->toggle_break) {
+        /* no longer breaking, so emulate idle in oversampled VAL register */
+        s->in_break = false;
+    }
+
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
+
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_event_handler(void *opaque, QEMUChrEvent event)
+{
+    OtUARTState *s = opaque;
+
+    if (event == CHR_EVENT_BREAK) {
+        if (!s->in_break || !s->oversample_break) {
+            /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+            s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+            ot_uart_update_irqs(s);
+            /* emulate break in the oversampled VAL register */
+            s->in_break = true;
+        } else if (s->toggle_break) {
+            /* emulate toggling break off in the oversampled VAL register */
+            s->in_break = false;
+        }
+    }
+}
+
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+}
+
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
+    }
+
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end of TX
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
+
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
+    }
+
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond,
+                                 void *opaque)
+{
+    OtUARTState *s = opaque;
+    (void)do_not_use;
+    (void)cond;
+
+    s->watch_tag = 0;
+    ot_uart_xmit(s);
+
+    return FALSE;
+}
+
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
+{
+    if (fifo8_is_full(&s->tx_fifo)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
+    }
+
+    fifo8_push(&s->tx_fifo, val);
+
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
+    }
+
+    if (ot_uart_is_tx_enabled(s)) {
+        ot_uart_xmit(s);
+    }
+}
+
+static void ot_uart_clock_input(void *opaque, int irq, int level)
+{
+    OtUARTState *s = opaque;
+
+    g_assert(irq == 0);
+

hw/char/ot_uart.c line 315

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    s->pclk = (unsigned)level;
+
+    /* TODO: disable UART transfer when PCLK is 0 */
+    ot_uart_check_baudrate(s);
+}
+
+static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;

Not sure this is required

Quoted 502 lines
hw/char/ot_uart.c new file +320 -0
+    uint32_t val32;
+
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
+    case R_INTR_STATE:
+    case R_INTR_ENABLE:
+    case R_CTRL:
+    case R_FIFO_CTRL:
+        val32 = s->regs[reg];
+        break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        val32 = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            val32 |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            val32 |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            val32 |= R_STATUS_TXIDLE_MASK;
+        }
+        if (!ot_uart_is_rx_enabled(s)) {
+            val32 |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
+    case R_RDATA:
+        val32 = (uint32_t)ot_uart_read_rx_fifo(s);
+        break;
+    case R_FIFO_STATUS:
+        val32 =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) << R_FIFO_STATUS_RXLVL_SHIFT;
+        val32 |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) << R_FIFO_STATUS_TXLVL_SHIFT;
+        break;
+    case R_VAL:
+        /*
+         * This is not trivially implemented due to the QEMU UART
+         * interface. There is no way to reliably sample or oversample
+         * given our emulated interface, but some software might poll the
+         * value of this register to determine break conditions.
+         *
+         * As such, default to reporting 16 of the last sample received
+         * instead. This defaults to 16 idle high samples (as a stop bit is
+         * always the last received), except for when the `oversample-break`
+         * property is set and a break condition is received over UART RX,
+         * where we then show 16 low samples until the next valid UART
+         * transmission is received (or break is toggled off with the
+         * `toggle-break` property enabled). This will not be accurate, but
+         * should be sufficient to support basic software flows that
+         * essentially use UART break as a strapping mechanism.
+         */
+        val32 = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX;
+        qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n", __func__,
+                      (s->oversample_break ? "/break" : ""));
+        break;
+    case R_OVRD:
+    case R_TIMEOUT_CTRL:
+        val32 = s->regs[reg];
+        qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__,
+                      REG_NAME(reg));
+        break;
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        val32 = 0;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        val32 = 0;
+        break;
+    }
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32,
+                              pc);
+
+    return (uint64_t)val32;
+}
+
+static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
+                          unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;
+    uint32_t val32 = val64;
+
+    hwaddr reg = R32_OFF(addr);
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32, pc);
+
+    switch (reg) {
+    case R_INTR_STATE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] &= ~val32; /* RW1C */
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_ENABLE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_ENABLE] = val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_TEST:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] |= val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_ALERT_TEST:
+        val32 &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = val32;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
+    case R_CTRL:
+        if (val32 & ~CTRL_SUP_MASK) {
+            qemu_log_mask(LOG_UNIMP,
+                          "%s: UART_CTRL feature not supported: 0x%08x\n",
+                          __func__, val32 & ~CTRL_SUP_MASK);
+        }
+        uint32_t prev = s->regs[R_CTRL];
+        s->regs[R_CTRL] = val32 & CTRL_MASK;
+        uint32_t change = prev ^ s->regs[R_CTRL];
+        if (change & R_CTRL_NCO_MASK) {
+            ot_uart_check_baudrate(s);
+        }
+        if ((change & R_CTRL_RX_MASK) && ot_uart_is_rx_enabled(s) &&
+            !ot_uart_is_sys_loopack_enabled(s)) {
+            qemu_chr_fe_accept_input(&s->chr);
+        }
+        if ((change & R_CTRL_TX_MASK) && ot_uart_is_tx_enabled(s)) {
+            /* try sending pending data from TX FIFO if any */
+            ot_uart_xmit(s);
+        }
+        break;
+    case R_WDATA:
+        uart_write_tx_fifo(s, (uint8_t)(val32 & R_WDATA_WDATA_MASK));
+        break;
+    case R_FIFO_CTRL:
+        s->regs[R_FIFO_CTRL] =
+            val32 & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
+        if (val32 & R_FIFO_CTRL_RXRST_MASK) {
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        if (val32 & R_FIFO_CTRL_TXRST_MASK) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        break;
+    case R_OVRD:
+        if (val32 & R_OVRD_TXEN_MASK) {
+            qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
+                          __func__);
+        }
+        s->regs[R_OVRD] = val32 & R_OVRD_TXVAL_MASK;
+        break;
+    case R_TIMEOUT_CTRL:
+        s->regs[R_TIMEOUT_CTRL] =
+            val32 & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
+        break;
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        break;
+    }
+}
+
+static const MemoryRegionOps ot_uart_ops = {
+    .read = ot_uart_read,
+    .write = ot_uart_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl.min_access_size = 4,
+    .impl.max_access_size = 4,
+};
+
+static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),
+    DEFINE_PROP_CHR("chardev", OtUARTState, chr),
+    DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
+    DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
+};
+
+static int ot_uart_be_change(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+
+    if (s->watch_tag > 0) {
+        g_source_remove(s->watch_tag);
+        /* NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) */
+        s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                             ot_uart_watch_cb, s);
+    }
+
+    return 0;
+}
+
+static void ot_uart_reset_enter(Object *obj, ResetType type)
+{
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
+
+    memset(&s->regs[0], 0, sizeof(s->regs));
+
+    s->tx_watermark_level = 0;
+    for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
+        qemu_set_irq(s->irqs[index], 0);
+    }
+    ot_uart_reset_tx_fifo(s);
+    ot_uart_reset_rx_fifo(s);
+
+    /*
+     * do not reset `s->in_break`, as that tracks whether we are currently
+     * receiving a break condition over UART RX from some device talking
+     * to OpenTitan, which should survive resets. The QEMU CharDev only
+     * supports transient break events and not the notion of holding the
+     * UART in break, so remembering breaks like this is required to
+     * support mocking of break conditions in the oversampled `VAL` reg.
+     */
+    if (s->in_break) {
+        /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+        s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_realize(DeviceState *dev, Error **errp)
+{
+    OtUARTState *s = OT_UART(dev);
+    (void)errp;
+
+    g_assert(s->ot_id);
+
+    qdev_init_gpio_in_named(DEVICE(s), &ot_uart_clock_input, "clock-in", 1);
+
+    fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE);
+    fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE);
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+}
+
+static void ot_uart_init(Object *obj)
+{
+    OtUARTState *s = OT_UART(obj);
+
+    for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) {
+        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[index]);
+    }
+
+    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
+                          REGS_SIZE);
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+}
+
+static void ot_uart_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    (void)data;
+
+    dc->realize = ot_uart_realize;
+    device_class_set_props(dc, ot_uart_properties);
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
+}
+
+static const TypeInfo ot_uart_info = {
+    .name = TYPE_OT_UART,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(OtUARTState),
+    .instance_init = ot_uart_init,
+    .class_size = sizeof(OtUARTClass),
+    .class_init = ot_uart_class_init,
+};
+
+static void ot_uart_register_types(void)
+{
+    type_register_static(&ot_uart_info);
+}
+
+type_init(ot_uart_register_types);
hw/char/trace-events +8 -0
index a3fcc77287..c859d8af4e 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned pclk, unsigned baud) "%s: @ %u Hz: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +2 -1
index 309125e854..163d3ac3d3 100644
@@ -132,7 +132,8 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ibex_uart.h deleted +0 -73
deleted file mode 100644
index 882796e0c6..0000000000
@@ -1,73 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef HW_IBEX_UART_H
-#define HW_IBEX_UART_H
-
-#include "hw/core/sysbus.h"
-#include "chardev/char-fe.h"
-#include "qemu/timer.h"
-#include "qom/object.h"
-
-#define IBEX_UART_TX_FIFO_SIZE 16
-#define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
-
-#define TYPE_IBEX_UART "ibex-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
-
-struct IbexUartState {
-    /* <private> */
-    SysBusDevice parent_obj;
-
-    /* <public> */
-    MemoryRegion mmio;
-
-    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
-    uint32_t tx_level;
-
-    uint32_t rx_level;
-
-    QEMUTimer *fifo_trigger_handle;
-    uint64_t char_tx_time;
-
-    uint32_t uart_intr_state;
-    uint32_t uart_intr_enable;
-    uint32_t uart_ctrl;
-    uint32_t uart_status;
-    uint32_t uart_rdata;
-    uint32_t uart_fifo_ctrl;
-    uint32_t uart_fifo_status;
-    uint32_t uart_ovrd;
-    uint32_t uart_val;
-    uint32_t uart_timeout_ctrl;
-
-    Clock *f_clk;
-
-    CharFrontend chr;
-    qemu_irq tx_watermark;
-    qemu_irq rx_watermark;
-    qemu_irq tx_empty;
-    qemu_irq rx_overflow;
-};
-#endif /* HW_IBEX_UART_H */
include/hw/char/ot_uart.h new file +67 -0
new file mode 100644
index 0000000000..45ec3fc7c2
@@ -0,0 +1,133 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ */
+
+#ifndef HW_OPENTITAN_OT_UART_H
+#define HW_OPENTITAN_OT_UART_H
+
+#include "qemu/fifo8.h"
+#include "chardev/char-fe.h"
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+#include "hw/core/registerfields.h"
+
+#define TYPE_OT_UART "ot-uart"
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
+
+/* clang-format off */
+REG32(INTR_STATE, 0x00u)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0u, 1u)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1u, 1u)
+    SHARED_FIELD(INTR_TX_DONE, 2u, 1u)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3u, 1u)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4u, 1u)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5u, 1u)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6u, 1u)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7u, 1u)
+    SHARED_FIELD(INTR_TX_EMPTY, 8u, 1u)
+REG32(INTR_ENABLE, 0x04u)
+REG32(INTR_TEST, 0x08u)
+REG32(ALERT_TEST, 0x0cu)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u)
+REG32(CTRL, 0x10u)
+    FIELD(CTRL, TX, 0u, 1u)
+    FIELD(CTRL, RX, 1u, 1u)
+    FIELD(CTRL, NF, 2u, 1u)
+    FIELD(CTRL, SLPBK, 4u, 1u)
+    FIELD(CTRL, LLPBK, 5u, 1u)
+    FIELD(CTRL, PARITY_EN, 6u, 1u)
+    FIELD(CTRL, PARITY_ODD, 7u, 1u)
+    FIELD(CTRL, RXBLVL, 8u, 2u)
+    FIELD(CTRL, NCO, 16u, 16u)
+REG32(STATUS, 0x14u)
+    FIELD(STATUS, TXFULL, 0u, 1u)
+    FIELD(STATUS, RXFULL, 1u, 1u)
+    FIELD(STATUS, TXEMPTY, 2u, 1u)
+    FIELD(STATUS, TXIDLE, 3u, 1u)
+    FIELD(STATUS, RXIDLE, 4u, 1u)
+    FIELD(STATUS, RXEMPTY, 5u, 1u)
+REG32(RDATA, 0x18u)
+    FIELD(RDATA, RDATA, 0u, 8u)
+REG32(WDATA, 0x1cu)
+    FIELD(WDATA, WDATA, 0u, 8u)
+REG32(FIFO_CTRL, 0x20u)
+    FIELD(FIFO_CTRL, RXRST, 0u, 1u)
+    FIELD(FIFO_CTRL, TXRST, 1u, 1u)
+    FIELD(FIFO_CTRL, RXILVL, 2u, 3u)
+    FIELD(FIFO_CTRL, TXILVL, 5u, 3u)
+REG32(FIFO_STATUS, 0x24u)
+    FIELD(FIFO_STATUS, TXLVL, 0u, 8u)

include/hw/char/ot_uart.h line 77

Quoted 10 lines
include/hw/char/ot_uart.h new file +10 -0
+    FIELD(FIFO_STATUS, RXLVL, 16u, 8u)
+REG32(OVRD, 0x28u)
+    FIELD(OVRD, TXEN, 0u, 1u)
+    FIELD(OVRD, TXVAL, 1u, 1u)
+REG32(VAL, 0x2cu)
+    FIELD(VAL, RX, 0u, 16u)
+REG32(TIMEOUT_CTRL, 0x30u)
+    FIELD(TIMEOUT_CTRL, VAL, 0u, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31u, 1u)
+/* clang-format on */

These shouldn't need to be public

Alistair

Quoted 81 lines
include/hw/char/ot_uart.h new file +56 -0
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define CTRL_SUP_MASK \
+    (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16u
+#define OT_UART_TX_FIFO_SIZE 128u
+#define OT_UART_RX_FIFO_SIZE 128u
+#define OT_UART_IRQ_NUM      9u
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?")
+
+struct OtUARTState {
+    SysBusDevice parent_obj;
+    MemoryRegion mmio;
+    qemu_irq irqs[OT_UART_IRQ_NUM];
+
+    uint32_t regs[REGS_COUNT];
+
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+    uint32_t tx_watermark_level;
+    bool in_break;
+    guint watch_tag;
+    unsigned pclk; /* Current input clock */
+    const char *clock_src_name; /* IRQ name once connected */
+
+    char *ot_id;
+    char *clock_name;
+    DeviceState *clock_src;
+    CharFrontend chr;
+    bool oversample_break; /* Should mock break in the oversampled VAL reg? */
+    bool toggle_break; /* Are incoming breaks temporary or toggled? */
+};
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
+#endif /* HW_OPENTITAN_OT_UART_H */
include/hw/riscv/opentitan.h +2 -3
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
 #include "hw/timer/ibex_timer.h"
 #include "hw/ssi/ibex_spi_host.h"
 #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
     IbexTimerState timer;
     IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
--

2.49.1

#3 lore raw

Re: [PATCH qemu 1/1] Replace opentitan UART device with newer version (opentitan 1.0)

Quoted 23 lines · Alistair Francis

On Fri, Mar 20, 2026 at 12:50 AM ~lexbaileylowrisc <[email protected]> wrote:
You need a commit message, it should describe what you are doing, link to specs and provide details for future readers.

Quoted 18 lines

From: Lex Bailey<[email protected]>

---
  hw/char/ibex_uart.c          | 569 -------------------------------
  hw/char/meson.build          |   2 +-
  hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
  hw/char/trace-events         |   8 +
  hw/riscv/opentitan.c         |   3 +-
  include/hw/char/ibex_uart.h  |  73 ----
  include/hw/char/ot_uart.h    | 133 ++++++++
  include/hw/riscv/opentitan.h |   4 +-
  8 files changed, 781 insertions(+), 646 deletions(-)

delete mode 100644 hw/char/ibex_uart.c
create mode 100644 hw/char/ot_uart.c
delete mode 100644 include/hw/char/ibex_uart.h
create mode 100644 include/hw/char/ot_uart.h

Quoted 9 lines

hw/char/ibex_uart.c

Quoted 5 lines
hw/char/ibex_uart.c deleted +0 -0
deleted file mode 100644
index 127d219df3..0000000000

I don't think we should remove the file. If you want to rename it that's fine, but that should be its own commit. So one commit to rename it (with a justification in the commit message) and then more commits to make changes.

okay, I will rename it first and then make changes

Quoted 615 lines
Quoted 615 lines
hw/char/ibex_uart.c deleted +0 -569
@@ -1,569 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * For details check the documentation here:
- *https://docs.opentitan.org/hw/ip/uart/doc/
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "qemu/osdep.h"
-#include "hw/char/ibex_uart.h"
-#include "hw/core/irq.h"
-#include "hw/core/qdev-clock.h"
-#include "hw/core/qdev-properties.h"
-#include "hw/core/qdev-properties-system.h"
-#include "hw/core/registerfields.h"
-#include "migration/vmstate.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
-
-REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
-REG32(INTR_ENABLE, 0x04)
-REG32(INTR_TEST, 0x08)
-REG32(ALERT_TEST, 0x0C)
-REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
-    FIELD(CTRL, NF, 2, 1)
-    FIELD(CTRL, SLPBK, 4, 1)
-    FIELD(CTRL, LLPBK, 5, 1)
-    FIELD(CTRL, PARITY_EN, 6, 1)
-    FIELD(CTRL, PARITY_ODD, 7, 1)
-    FIELD(CTRL, RXBLVL, 8, 2)
-    FIELD(CTRL, NCO, 16, 16)
-REG32(STATUS, 0x14)
-    FIELD(STATUS, TXFULL, 0, 1)
-    FIELD(STATUS, RXFULL, 1, 1)
-    FIELD(STATUS, TXEMPTY, 2, 1)
-    FIELD(STATUS, RXIDLE, 4, 1)
-    FIELD(STATUS, RXEMPTY, 5, 1)
-REG32(RDATA, 0x18)
-REG32(WDATA, 0x1C)
-REG32(FIFO_CTRL, 0x20)
-    FIELD(FIFO_CTRL, RXRST, 0, 1)
-    FIELD(FIFO_CTRL, TXRST, 1, 1)
-    FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
-REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
-REG32(OVRD, 0x28)
-REG32(VAL, 0x2C)
-REG32(TIMEOUT_CTRL, 0x30)
-
-static void ibex_uart_update_irqs(IbexUartState *s)
-{
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
-    }
-}
-
-static int ibex_uart_can_receive(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
-    }
-
-    return 0;
-}
-
-static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
-{
-    IbexUartState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
-
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
-
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-}
-
-static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
-                               void *opaque)
-{
-    IbexUartState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
-    }
-
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
-    }
-
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
-
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
-
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ibex_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
-    }
-
-    /* Clear the TX Full bit */
-    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-    }
-
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-    }
-
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
-}
-
-static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
-                               int size)
-{
-    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
-        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
-    }
-
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
-
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
-    }
-
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
-    }
-
-    timer_mod(s->fifo_trigger_handle, current_time +
-              (s->char_tx_time * 4));
-}
-
-static void ibex_uart_reset(DeviceState *dev)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
-
-    s->tx_level = 0;
-    s->rx_level = 0;
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
-
-    ibex_uart_update_irqs(s);
-}
-
-static uint64_t ibex_uart_get_baud(IbexUartState *s)
-{
-    uint64_t baud;
-
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
-    baud *= clock_get_hz(s->f_clk);
-    baud >>= 20;
-
-    return baud;
-}
-
-static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
-                                       unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint64_t retvalue = 0;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
-        break;
-    case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_CTRL:
-        retvalue = s->uart_ctrl;
-        break;
-    case R_STATUS:
-        retvalue = s->uart_status;
-        break;
-
-    case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
-        break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
-        break;
-    case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
-        break;
-
-    case R_OVRD:
-        retvalue = s->uart_ovrd;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        retvalue = s->uart_val;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-        return 0;
-    }
-
-    return retvalue;
-}
-
-static void ibex_uart_write(void *opaque, hwaddr addr,
-                                  uint64_t val64, unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint32_t value = val64;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        /* Write 1 clear */
-        s->uart_intr_state &= ~value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_TEST:
-        s->uart_intr_state |= value;
-        ibex_uart_update_irqs(s);
-        break;
-
-    case R_CTRL:
-        s->uart_ctrl = value;
-
-        if (value & R_CTRL_NF_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_NF is not supported\n", __func__);
-        }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_LLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_LLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_PARITY_EN_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_EN is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_PARITY_ODD_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_RXBLVL_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
-        }
-        if (value & R_CTRL_NCO_MASK) {
-            uint64_t baud = ibex_uart_get_baud(s);
-
-            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-        }
-        break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
-    case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
-        break;
-
-    case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
-
-        if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
-        }
-        if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
-        }
-        break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
-
-    case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-    }
-}
-
-static void ibex_uart_clk_update(void *opaque, ClockEvent event)
-{
-    IbexUartState *s = opaque;
-
-    /* recompute uart's speed on clock change */
-    uint64_t baud = ibex_uart_get_baud(s);
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-}
-
-static void fifo_trigger_update(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ibex_uart_xmit(NULL, G_IO_OUT, s);
-    }
-}
-
-static const MemoryRegionOps ibex_uart_ops = {
-    .read = ibex_uart_read,
-    .write = ibex_uart_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
-    .impl.min_access_size = 4,
-    .impl.max_access_size = 4,
-};
-
-static int ibex_uart_post_load(void *opaque, int version_id)
-{
-    IbexUartState *s = opaque;
-
-    ibex_uart_update_irqs(s);
-    return 0;
-}
-
-static const VMStateDescription vmstate_ibex_uart = {
-    .name = TYPE_IBEX_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .post_load = ibex_uart_post_load,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
-                            IBEX_UART_TX_FIFO_SIZE),
-        VMSTATE_UINT32(tx_level, IbexUartState),
-        VMSTATE_UINT64(char_tx_time, IbexUartState),
-        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
-        VMSTATE_UINT32(uart_intr_state, IbexUartState),
-        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
-        VMSTATE_UINT32(uart_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_status, IbexUartState),
-        VMSTATE_UINT32(uart_rdata, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
-        VMSTATE_UINT32(uart_ovrd, IbexUartState),
-        VMSTATE_UINT32(uart_val, IbexUartState),
-        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const Property ibex_uart_properties[] = {
-    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
-};
-
-static void ibex_uart_init(Object *obj)
-{
-    IbexUartState *s = IBEX_UART(obj);
-
-    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
-                                  ibex_uart_clk_update, s, ClockUpdate);
-    clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
-
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
-
-    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
-                          TYPE_IBEX_UART, 0x400);
-    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
-}
-
-static void ibex_uart_realize(DeviceState *dev, Error **errp)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                          fifo_trigger_update, s);
-
-    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
-                             ibex_uart_receive, NULL, NULL,
-                             s, NULL, true);
-}
-
-static void ibex_uart_class_init(ObjectClass *klass, const void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-
-    device_class_set_legacy_reset(dc, ibex_uart_reset);
-    dc->realize = ibex_uart_realize;
-    dc->vmsd = &vmstate_ibex_uart;
-    device_class_set_props(dc, ibex_uart_properties);
-    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
-}
-
-static const TypeInfo ibex_uart_info = {
-    .name          = TYPE_IBEX_UART,
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(IbexUartState),
-    .instance_init = ibex_uart_init,
-    .class_init    = ibex_uart_class_init,
-};
-
-static void ibex_uart_register_types(void)
-{
-    type_register_static(&ibex_uart_info);
-}
-
-type_init(ibex_uart_register_types)
hw/char/meson.build +1 -1
index fc3d7ee506..989ca3d7e0 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))
  system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true: files('cmsdk-apb-uart.c'))
  system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
  system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_IBEX', if_true: files('ot_uart.c'))
  system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
  system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true: files('ipoctal232.c'))
  system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c new file +25 -0
new file mode 100644
index 0000000000..757f64ab70
@@ -0,0 +1,635 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025 lowRISC contributors.
+ *
+ * Author(s):
+ *  Loïc Lefort<[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Based on original ibex_uart implementation:
+ *  Copyright (c) 2020 Western Digital
+ *  Alistair Francis<[email protected]>
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/fifo8.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+#include "chardev/char-fe.h"
+#include "hw/core/cpu.h"
+#include "hw/core/irq.h"
Quoted 8 lines

hw/char/ot_uart.c line 32

Quoted 7 lines
hw/char/ot_uart.c new file +7 -0
+#include "hw/char/ot_uart.h"
+#include "hw/core/qdev-properties-system.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/registerfields.h"
+#include "trace.h"
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)

Let's use the existing QEMU macros instead of creating custom ones.

Specifically which macro should I be using instead? I can't find any that do the same as this one. It is used to populate the array of register names just below, which are used in log/trace messages

Quoted 797 lines
Quoted 14 lines
hw/char/ot_uart.c new file +13 -0
+static const char *REG_NAMES[REGS_COUNT] = {
+    /* clang-format off */
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),

hw/char/ot_uart.c line 55

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+    /* clang-format on */
+};
+#undef REG_NAME_ENTRY
+
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_TXILVL_MASK) >>
+                       R_FIFO_CTRL_TXILVL_SHIFT;

You can then use the extract (FIELD_EX32) macros instead of mask and shift.

hw/char/ot_uart.c line 57

Quoted 2 lines
hw/char/ot_uart.c new file +2 -0
+
+    return tx_ilvl < 7u ? (1u << tx_ilvl) : 64u;

You shouldn't need the us

Quoted 16 lines
hw/char/ot_uart.c new file +16 -0
+}
+
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_RXILVL_MASK) >>
+                       R_FIFO_CTRL_RXILVL_SHIFT;
+
+    return rx_ilvl < 7u ? (1u << rx_ilvl) : 126u;
+}
+
+static void ot_uart_update_irqs(OtUARTState *s)
+{
+    uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
+
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE],
+                       state_masked);

hw/char/ot_uart.c line 83

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+
+    for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
+        bool level = (state_masked & (1U << index)) != 0;
+        qemu_set_irq(s->irqs[index], level);
+    }
+}
+
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);

You also should need to cast this

Quoted 222 lines
hw/char/ot_uart.c new file +222 -0
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
+static void ot_uart_check_baudrate(const OtUARTState *s)
+{
+    uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO);
+
+    unsigned baudrate = (unsigned)(((uint64_t)nco * (uint64_t)s->pclk) >>
+                                   (R_CTRL_NCO_LENGTH + 4));
+
+    if (baudrate) {
+        trace_ot_uart_check_baudrate(s->ot_id, s->pclk, baudrate);
+    }
+}
+
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+}
+
+static int ot_uart_can_receive(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
+    }
+
+    return 0;
+}
+
+static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    OtUARTState *s = opaque;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
+
+    if (size && !s->toggle_break) {
+        /* no longer breaking, so emulate idle in oversampled VAL register */
+        s->in_break = false;
+    }
+
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
+
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_event_handler(void *opaque, QEMUChrEvent event)
+{
+    OtUARTState *s = opaque;
+
+    if (event == CHR_EVENT_BREAK) {
+        if (!s->in_break || !s->oversample_break) {
+            /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+            s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+            ot_uart_update_irqs(s);
+            /* emulate break in the oversampled VAL register */
+            s->in_break = true;
+        } else if (s->toggle_break) {
+            /* emulate toggling break off in the oversampled VAL register */
+            s->in_break = false;
+        }
+    }
+}
+
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+}
+
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
+    }
+
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end of TX
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
+
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
+    }
+
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond,
+                                 void *opaque)
+{
+    OtUARTState *s = opaque;
+    (void)do_not_use;
+    (void)cond;
+
+    s->watch_tag = 0;
+    ot_uart_xmit(s);
+
+    return FALSE;
+}
+
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
+{
+    if (fifo8_is_full(&s->tx_fifo)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
+    }
+
+    fifo8_push(&s->tx_fifo, val);
+
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
+    }
+
+    if (ot_uart_is_tx_enabled(s)) {
+        ot_uart_xmit(s);
+    }
+}
+
+static void ot_uart_clock_input(void *opaque, int irq, int level)
+{
+    OtUARTState *s = opaque;
+
+    g_assert(irq == 0);
+

hw/char/ot_uart.c line 315

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    s->pclk = (unsigned)level;
+
+    /* TODO: disable UART transfer when PCLK is 0 */
+    ot_uart_check_baudrate(s);
+}
+
+static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;

Not sure this is required

Quoted 505 lines
hw/char/ot_uart.c new file +320 -0
+    uint32_t val32;
+
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
+    case R_INTR_STATE:
+    case R_INTR_ENABLE:
+    case R_CTRL:
+    case R_FIFO_CTRL:
+        val32 = s->regs[reg];
+        break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        val32 = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            val32 |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            val32 |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            val32 |= R_STATUS_TXIDLE_MASK;
+        }
+        if (!ot_uart_is_rx_enabled(s)) {
+            val32 |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
+    case R_RDATA:
+        val32 = (uint32_t)ot_uart_read_rx_fifo(s);
+        break;
+    case R_FIFO_STATUS:
+        val32 =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) << R_FIFO_STATUS_RXLVL_SHIFT;
+        val32 |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) << R_FIFO_STATUS_TXLVL_SHIFT;
+        break;
+    case R_VAL:
+        /*
+         * This is not trivially implemented due to the QEMU UART
+         * interface. There is no way to reliably sample or oversample
+         * given our emulated interface, but some software might poll the
+         * value of this register to determine break conditions.
+         *
+         * As such, default to reporting 16 of the last sample received
+         * instead. This defaults to 16 idle high samples (as a stop bit is
+         * always the last received), except for when the `oversample-break`
+         * property is set and a break condition is received over UART RX,
+         * where we then show 16 low samples until the next valid UART
+         * transmission is received (or break is toggled off with the
+         * `toggle-break` property enabled). This will not be accurate, but
+         * should be sufficient to support basic software flows that
+         * essentially use UART break as a strapping mechanism.
+         */
+        val32 = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX;
+        qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n", __func__,
+                      (s->oversample_break ? "/break" : ""));
+        break;
+    case R_OVRD:
+    case R_TIMEOUT_CTRL:
+        val32 = s->regs[reg];
+        qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__,
+                      REG_NAME(reg));
+        break;
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        val32 = 0;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        val32 = 0;
+        break;
+    }
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32,
+                              pc);
+
+    return (uint64_t)val32;
+}
+
+static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
+                          unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;
+    uint32_t val32 = val64;
+
+    hwaddr reg = R32_OFF(addr);
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32, pc);
+
+    switch (reg) {
+    case R_INTR_STATE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] &= ~val32; /* RW1C */
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_ENABLE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_ENABLE] = val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_TEST:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] |= val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_ALERT_TEST:
+        val32 &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = val32;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
+    case R_CTRL:
+        if (val32 & ~CTRL_SUP_MASK) {
+            qemu_log_mask(LOG_UNIMP,
+                          "%s: UART_CTRL feature not supported: 0x%08x\n",
+                          __func__, val32 & ~CTRL_SUP_MASK);
+        }
+        uint32_t prev = s->regs[R_CTRL];
+        s->regs[R_CTRL] = val32 & CTRL_MASK;
+        uint32_t change = prev ^ s->regs[R_CTRL];
+        if (change & R_CTRL_NCO_MASK) {
+            ot_uart_check_baudrate(s);
+        }
+        if ((change & R_CTRL_RX_MASK) && ot_uart_is_rx_enabled(s) &&
+            !ot_uart_is_sys_loopack_enabled(s)) {
+            qemu_chr_fe_accept_input(&s->chr);
+        }
+        if ((change & R_CTRL_TX_MASK) && ot_uart_is_tx_enabled(s)) {
+            /* try sending pending data from TX FIFO if any */
+            ot_uart_xmit(s);
+        }
+        break;
+    case R_WDATA:
+        uart_write_tx_fifo(s, (uint8_t)(val32 & R_WDATA_WDATA_MASK));
+        break;
+    case R_FIFO_CTRL:
+        s->regs[R_FIFO_CTRL] =
+            val32 & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
+        if (val32 & R_FIFO_CTRL_RXRST_MASK) {
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        if (val32 & R_FIFO_CTRL_TXRST_MASK) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        break;
+    case R_OVRD:
+        if (val32 & R_OVRD_TXEN_MASK) {
+            qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
+                          __func__);
+        }
+        s->regs[R_OVRD] = val32 & R_OVRD_TXVAL_MASK;
+        break;
+    case R_TIMEOUT_CTRL:
+        s->regs[R_TIMEOUT_CTRL] =
+            val32 & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
+        break;
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        break;
+    }
+}
+
+static const MemoryRegionOps ot_uart_ops = {
+    .read = ot_uart_read,
+    .write = ot_uart_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl.min_access_size = 4,
+    .impl.max_access_size = 4,
+};
+
+static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),
+    DEFINE_PROP_CHR("chardev", OtUARTState, chr),
+    DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
+    DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
+};
+
+static int ot_uart_be_change(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+
+    if (s->watch_tag > 0) {
+        g_source_remove(s->watch_tag);
+        /* NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) */
+        s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                             ot_uart_watch_cb, s);
+    }
+
+    return 0;
+}
+
+static void ot_uart_reset_enter(Object *obj, ResetType type)
+{
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
+
+    memset(&s->regs[0], 0, sizeof(s->regs));
+
+    s->tx_watermark_level = 0;
+    for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
+        qemu_set_irq(s->irqs[index], 0);
+    }
+    ot_uart_reset_tx_fifo(s);
+    ot_uart_reset_rx_fifo(s);
+
+    /*
+     * do not reset `s->in_break`, as that tracks whether we are currently
+     * receiving a break condition over UART RX from some device talking
+     * to OpenTitan, which should survive resets. The QEMU CharDev only
+     * supports transient break events and not the notion of holding the
+     * UART in break, so remembering breaks like this is required to
+     * support mocking of break conditions in the oversampled `VAL` reg.
+     */
+    if (s->in_break) {
+        /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+        s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_realize(DeviceState *dev, Error **errp)
+{
+    OtUARTState *s = OT_UART(dev);
+    (void)errp;
+
+    g_assert(s->ot_id);
+
+    qdev_init_gpio_in_named(DEVICE(s), &ot_uart_clock_input, "clock-in", 1);
+
+    fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE);
+    fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE);
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+}
+
+static void ot_uart_init(Object *obj)
+{
+    OtUARTState *s = OT_UART(obj);
+
+    for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) {
+        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[index]);
+    }
+
+    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
+                          REGS_SIZE);
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+}
+
+static void ot_uart_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    (void)data;
+
+    dc->realize = ot_uart_realize;
+    device_class_set_props(dc, ot_uart_properties);
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
+}
+
+static const TypeInfo ot_uart_info = {
+    .name = TYPE_OT_UART,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(OtUARTState),
+    .instance_init = ot_uart_init,
+    .class_size = sizeof(OtUARTClass),
+    .class_init = ot_uart_class_init,
+};
+
+static void ot_uart_register_types(void)
+{
+    type_register_static(&ot_uart_info);
+}
+
+type_init(ot_uart_register_types);
hw/char/trace-events +8 -0
index a3fcc77287..c859d8af4e 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
  # riscv_htif.c
  htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
  htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned pclk, unsigned baud) "%s: @ %u Hz: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s:act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +2 -1
index 309125e854..163d3ac3d3 100644
@@ -132,7 +132,8 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
      object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
      object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ibex_uart.h deleted +0 -73
deleted file mode 100644
index 882796e0c6..0000000000
@@ -1,73 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef HW_IBEX_UART_H
-#define HW_IBEX_UART_H
-
-#include "hw/core/sysbus.h"
-#include "chardev/char-fe.h"
-#include "qemu/timer.h"
-#include "qom/object.h"
-
-#define IBEX_UART_TX_FIFO_SIZE 16
-#define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
-
-#define TYPE_IBEX_UART "ibex-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
-
-struct IbexUartState {
-    /* <private> */
-    SysBusDevice parent_obj;
-
-    /* <public> */
-    MemoryRegion mmio;
-
-    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
-    uint32_t tx_level;
-
-    uint32_t rx_level;
-
-    QEMUTimer *fifo_trigger_handle;
-    uint64_t char_tx_time;
-
-    uint32_t uart_intr_state;
-    uint32_t uart_intr_enable;
-    uint32_t uart_ctrl;
-    uint32_t uart_status;
-    uint32_t uart_rdata;
-    uint32_t uart_fifo_ctrl;
-    uint32_t uart_fifo_status;
-    uint32_t uart_ovrd;
-    uint32_t uart_val;
-    uint32_t uart_timeout_ctrl;
-
-    Clock *f_clk;
-
-    CharFrontend chr;
-    qemu_irq tx_watermark;
-    qemu_irq rx_watermark;
-    qemu_irq tx_empty;
-    qemu_irq rx_overflow;
-};
-#endif /* HW_IBEX_UART_H */
include/hw/char/ot_uart.h new file +70 -0
new file mode 100644
index 0000000000..45ec3fc7c2
@@ -0,0 +1,133 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ *
+ * Author(s):
+ *  Loïc Lefort<[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ */
+
+#ifndef HW_OPENTITAN_OT_UART_H
+#define HW_OPENTITAN_OT_UART_H
+
+#include "qemu/fifo8.h"
+#include "chardev/char-fe.h"
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+#include "hw/core/registerfields.h"
+
+#define TYPE_OT_UART "ot-uart"
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
+
+/* clang-format off */
+REG32(INTR_STATE, 0x00u)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0u, 1u)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1u, 1u)
+    SHARED_FIELD(INTR_TX_DONE, 2u, 1u)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3u, 1u)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4u, 1u)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5u, 1u)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6u, 1u)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7u, 1u)
+    SHARED_FIELD(INTR_TX_EMPTY, 8u, 1u)
+REG32(INTR_ENABLE, 0x04u)
+REG32(INTR_TEST, 0x08u)
+REG32(ALERT_TEST, 0x0cu)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u)
+REG32(CTRL, 0x10u)
+    FIELD(CTRL, TX, 0u, 1u)
+    FIELD(CTRL, RX, 1u, 1u)
+    FIELD(CTRL, NF, 2u, 1u)
+    FIELD(CTRL, SLPBK, 4u, 1u)
+    FIELD(CTRL, LLPBK, 5u, 1u)
+    FIELD(CTRL, PARITY_EN, 6u, 1u)
+    FIELD(CTRL, PARITY_ODD, 7u, 1u)
+    FIELD(CTRL, RXBLVL, 8u, 2u)
+    FIELD(CTRL, NCO, 16u, 16u)
+REG32(STATUS, 0x14u)
+    FIELD(STATUS, TXFULL, 0u, 1u)
+    FIELD(STATUS, RXFULL, 1u, 1u)
+    FIELD(STATUS, TXEMPTY, 2u, 1u)
+    FIELD(STATUS, TXIDLE, 3u, 1u)
+    FIELD(STATUS, RXIDLE, 4u, 1u)
+    FIELD(STATUS, RXEMPTY, 5u, 1u)
+REG32(RDATA, 0x18u)
+    FIELD(RDATA, RDATA, 0u, 8u)
+REG32(WDATA, 0x1cu)
+    FIELD(WDATA, WDATA, 0u, 8u)
+REG32(FIFO_CTRL, 0x20u)
+    FIELD(FIFO_CTRL, RXRST, 0u, 1u)
+    FIELD(FIFO_CTRL, TXRST, 1u, 1u)
+    FIELD(FIFO_CTRL, RXILVL, 2u, 3u)
+    FIELD(FIFO_CTRL, TXILVL, 5u, 3u)
+REG32(FIFO_STATUS, 0x24u)
+    FIELD(FIFO_STATUS, TXLVL, 0u, 8u)
+    FIELD(FIFO_STATUS, RXLVL, 16u, 8u)
+REG32(OVRD, 0x28u)
+    FIELD(OVRD, TXEN, 0u, 1u)
Quoted 8 lines

include/hw/char/ot_uart.h line 77

Quoted 7 lines
include/hw/char/ot_uart.h new file +7 -0
+    FIELD(OVRD, TXVAL, 1u, 1u)
+REG32(VAL, 0x2cu)
+    FIELD(VAL, RX, 0u, 16u)
+REG32(TIMEOUT_CTRL, 0x30u)
+    FIELD(TIMEOUT_CTRL, VAL, 0u, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31u, 1u)
+/* clang-format on */

These shouldn't need to be public

agreed. They are here because the struct definition below uses REGS_COUNT which is computed based on these definitions. I can hard-code the length of 13 instead? It looks like other hardware blocks also hard-code this, is there some way to at least catch the error if this stops being self-consistent?

Quoted 84 lines

Alistair

Quoted 81 lines
include/hw/char/ot_uart.h new file +56 -0
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define CTRL_SUP_MASK \
+    (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16u
+#define OT_UART_TX_FIFO_SIZE 128u
+#define OT_UART_RX_FIFO_SIZE 128u
+#define OT_UART_IRQ_NUM      9u
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?")
+
+struct OtUARTState {
+    SysBusDevice parent_obj;
+    MemoryRegion mmio;
+    qemu_irq irqs[OT_UART_IRQ_NUM];
+
+    uint32_t regs[REGS_COUNT];
+
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+    uint32_t tx_watermark_level;
+    bool in_break;
+    guint watch_tag;
+    unsigned pclk; /* Current input clock */
+    const char *clock_src_name; /* IRQ name once connected */
+
+    char *ot_id;
+    char *clock_name;
+    DeviceState *clock_src;
+    CharFrontend chr;
+    bool oversample_break; /* Should mock break in the oversampled VAL reg? */
+    bool toggle_break; /* Are incoming breaks temporary or toggled? */
+};
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
+#endif /* HW_OPENTITAN_OT_UART_H */
include/hw/riscv/opentitan.h +2 -3
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
  #include "hw/riscv/riscv_hart.h"
  #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
  #include "hw/timer/ibex_timer.h"
  #include "hw/ssi/ibex_spi_host.h"
  #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
      /*< public >*/
      RISCVHartArrayState cpus;
      SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
      IbexTimerState timer;
      IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
--

2.49.1

Signature · 2 lines
Lex Bailey (they/them)
lowRISC C.I.C. 7 Hills Road, Cambridge, CB2 1GE, UK. Registered in England & Wales 09272283 [email protected]
#4 lore raw

Re: [PATCH qemu 1/1] Replace opentitan UART device with newer version (opentitan 1.0)

Quoted 663 lines · Lex Bailey

On 26/03/2026 02:39, Alistair Francis wrote:

On Fri, Mar 20, 2026 at 12:50 AM ~lexbaileylowrisc <[email protected]> wrote:

You need a commit message, it should describe what you are doing, link to specs and provide details for future readers.

From: Lex Bailey <[email protected]>

---
 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
hw/char/ibex_uart.c deleted +0 -0
deleted file mode 100644
index 127d219df3..0000000000

I don't think we should remove the file. If you want to rename it that's fine, but that should be its own commit. So one commit to rename it (with a justification in the commit message) and then more commits to make changes.

okay, I will rename it first and then make changes

hw/char/ibex_uart.c deleted +0 -569
@@ -1,569 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * For details check the documentation here:
- *    https://docs.opentitan.org/hw/ip/uart/doc/
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "qemu/osdep.h"
-#include "hw/char/ibex_uart.h"
-#include "hw/core/irq.h"
-#include "hw/core/qdev-clock.h"
-#include "hw/core/qdev-properties.h"
-#include "hw/core/qdev-properties-system.h"
-#include "hw/core/registerfields.h"
-#include "migration/vmstate.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
-
-REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
-REG32(INTR_ENABLE, 0x04)
-REG32(INTR_TEST, 0x08)
-REG32(ALERT_TEST, 0x0C)
-REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
-    FIELD(CTRL, NF, 2, 1)
-    FIELD(CTRL, SLPBK, 4, 1)
-    FIELD(CTRL, LLPBK, 5, 1)
-    FIELD(CTRL, PARITY_EN, 6, 1)
-    FIELD(CTRL, PARITY_ODD, 7, 1)
-    FIELD(CTRL, RXBLVL, 8, 2)
-    FIELD(CTRL, NCO, 16, 16)
-REG32(STATUS, 0x14)
-    FIELD(STATUS, TXFULL, 0, 1)
-    FIELD(STATUS, RXFULL, 1, 1)
-    FIELD(STATUS, TXEMPTY, 2, 1)
-    FIELD(STATUS, RXIDLE, 4, 1)
-    FIELD(STATUS, RXEMPTY, 5, 1)
-REG32(RDATA, 0x18)
-REG32(WDATA, 0x1C)
-REG32(FIFO_CTRL, 0x20)
-    FIELD(FIFO_CTRL, RXRST, 0, 1)
-    FIELD(FIFO_CTRL, TXRST, 1, 1)
-    FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
-REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
-REG32(OVRD, 0x28)
-REG32(VAL, 0x2C)
-REG32(TIMEOUT_CTRL, 0x30)
-
-static void ibex_uart_update_irqs(IbexUartState *s)
-{
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
-    }
-}
-
-static int ibex_uart_can_receive(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
-    }
-
-    return 0;
-}
-
-static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
-{
-    IbexUartState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
-
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
-
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-}
-
-static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
-                               void *opaque)
-{
-    IbexUartState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
-    }
-
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
-    }
-
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
-
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
-
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ibex_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
-    }
-
-    /* Clear the TX Full bit */
-    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-    }
-
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-    }
-
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-    }
-
-    ibex_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
-}
-
-static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
-                               int size)
-{
-    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
-        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
-    }
-
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
-
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
-    }
-
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
-    }
-
-    timer_mod(s->fifo_trigger_handle, current_time +
-              (s->char_tx_time * 4));
-}
-
-static void ibex_uart_reset(DeviceState *dev)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
-
-    s->tx_level = 0;
-    s->rx_level = 0;
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
-
-    ibex_uart_update_irqs(s);
-}
-
-static uint64_t ibex_uart_get_baud(IbexUartState *s)
-{
-    uint64_t baud;
-
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
-    baud *= clock_get_hz(s->f_clk);
-    baud >>= 20;
-
-    return baud;
-}
-
-static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
-                                       unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint64_t retvalue = 0;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
-        break;
-    case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_CTRL:
-        retvalue = s->uart_ctrl;
-        break;
-    case R_STATUS:
-        retvalue = s->uart_status;
-        break;
-
-    case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
-        break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
-        break;
-    case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
-        break;
-
-    case R_OVRD:
-        retvalue = s->uart_ovrd;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        retvalue = s->uart_val;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-        return 0;
-    }
-
-    return retvalue;
-}
-
-static void ibex_uart_write(void *opaque, hwaddr addr,
-                                  uint64_t val64, unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint32_t value = val64;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        /* Write 1 clear */
-        s->uart_intr_state &= ~value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_TEST:
-        s->uart_intr_state |= value;
-        ibex_uart_update_irqs(s);
-        break;
-
-    case R_CTRL:
-        s->uart_ctrl = value;
-
-        if (value & R_CTRL_NF_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_NF is not supported\n", __func__);
-        }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_LLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_LLPBK is not supported\n", __func__);
-        }
-        if (value & R_CTRL_PARITY_EN_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_EN is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_PARITY_ODD_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_RXBLVL_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
-        }
-        if (value & R_CTRL_NCO_MASK) {
-            uint64_t baud = ibex_uart_get_baud(s);
-
-            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-        }
-        break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
-    case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
-        break;
-
-    case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
-
-        if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
-        }
-        if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
-        }
-        break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
-
-    case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
-    }
-}
-
-static void ibex_uart_clk_update(void *opaque, ClockEvent event)
-{
-    IbexUartState *s = opaque;
-
-    /* recompute uart's speed on clock change */
-    uint64_t baud = ibex_uart_get_baud(s);
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-}
-
-static void fifo_trigger_update(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ibex_uart_xmit(NULL, G_IO_OUT, s);
-    }
-}
-
-static const MemoryRegionOps ibex_uart_ops = {
-    .read = ibex_uart_read,
-    .write = ibex_uart_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
-    .impl.min_access_size = 4,
-    .impl.max_access_size = 4,
-};
-
-static int ibex_uart_post_load(void *opaque, int version_id)
-{
-    IbexUartState *s = opaque;
-
-    ibex_uart_update_irqs(s);
-    return 0;
-}
-
-static const VMStateDescription vmstate_ibex_uart = {
-    .name = TYPE_IBEX_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .post_load = ibex_uart_post_load,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
-                            IBEX_UART_TX_FIFO_SIZE),
-        VMSTATE_UINT32(tx_level, IbexUartState),
-        VMSTATE_UINT64(char_tx_time, IbexUartState),
-        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
-        VMSTATE_UINT32(uart_intr_state, IbexUartState),
-        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
-        VMSTATE_UINT32(uart_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_status, IbexUartState),
-        VMSTATE_UINT32(uart_rdata, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
-        VMSTATE_UINT32(uart_ovrd, IbexUartState),
-        VMSTATE_UINT32(uart_val, IbexUartState),
-        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const Property ibex_uart_properties[] = {
-    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
-};
-
-static void ibex_uart_init(Object *obj)
-{
-    IbexUartState *s = IBEX_UART(obj);
-
-    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
-                                  ibex_uart_clk_update, s, ClockUpdate);
-    clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
-
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
-
-    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
-                          TYPE_IBEX_UART, 0x400);
-    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
-}
-
-static void ibex_uart_realize(DeviceState *dev, Error **errp)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                          fifo_trigger_update, s);
-
-    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
-                             ibex_uart_receive, NULL, NULL,
-                             s, NULL, true);
-}
-
-static void ibex_uart_class_init(ObjectClass *klass, const void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-
-    device_class_set_legacy_reset(dc, ibex_uart_reset);
-    dc->realize = ibex_uart_realize;
-    dc->vmsd = &vmstate_ibex_uart;
-    device_class_set_props(dc, ibex_uart_properties);
-    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
-}
-
-static const TypeInfo ibex_uart_info = {
-    .name          = TYPE_IBEX_UART,
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(IbexUartState),
-    .instance_init = ibex_uart_init,
-    .class_init    = ibex_uart_class_init,
-};
-
-static void ibex_uart_register_types(void)
-{
-    type_register_static(&ibex_uart_info);
-}
-
-type_init(ibex_uart_register_types)
hw/char/meson.build +1 -1
index fc3d7ee506..989ca3d7e0 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))
 system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true: files('cmsdk-apb-uart.c'))
 system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
 system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_IBEX', if_true: files('ot_uart.c'))
 system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
 system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true: files('ipoctal232.c'))
 system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c new file +32 -0
new file mode 100644
index 0000000000..757f64ab70
@@ -0,0 +1,635 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025 lowRISC contributors.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Based on original ibex_uart implementation:
+ *  Copyright (c) 2020 Western Digital
+ *  Alistair Francis <[email protected]>
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/fifo8.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+#include "chardev/char-fe.h"
+#include "hw/core/cpu.h"
+#include "hw/core/irq.h"
+#include "hw/char/ot_uart.h"
+#include "hw/core/qdev-properties-system.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/registerfields.h"
+#include "trace.h"
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)
Quoted 3 lines

Let's use the existing QEMU macros instead of creating custom ones.

Specifically which macro should I be using instead? I can't find any that do the same as this one. It is used to populate the array of register names just below, which are used in log/trace messages

Ah sorry. I originally thought this was instead of the REG32 macros, hence the comment. Then forgot to come back and delete the comment before sending.

Quoted 809 lines
hw/char/ot_uart.c new file +23 -0
+static const char *REG_NAMES[REGS_COUNT] = {
+    /* clang-format off */
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+    /* clang-format on */
+};
+#undef REG_NAME_ENTRY
+
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_TXILVL_MASK) >>
+                       R_FIFO_CTRL_TXILVL_SHIFT;

You can then use the extract (FIELD_EX32) macros instead of mask and shift.

hw/char/ot_uart.c new file +2 -0
+
+    return tx_ilvl < 7u ? (1u << tx_ilvl) : 64u;

You shouldn't need the us

hw/char/ot_uart.c new file +26 -0
+}
+
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_RXILVL_MASK) >>
+                       R_FIFO_CTRL_RXILVL_SHIFT;
+
+    return rx_ilvl < 7u ? (1u << rx_ilvl) : 126u;
+}
+
+static void ot_uart_update_irqs(OtUARTState *s)
+{
+    uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
+
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE],
+                       state_masked);
+
+    for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
+        bool level = (state_masked & (1U << index)) != 0;
+        qemu_set_irq(s->irqs[index], level);
+    }
+}
+
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);

You also should need to cast this

hw/char/ot_uart.c new file +232 -0
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
+static void ot_uart_check_baudrate(const OtUARTState *s)
+{
+    uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO);
+
+    unsigned baudrate = (unsigned)(((uint64_t)nco * (uint64_t)s->pclk) >>
+                                   (R_CTRL_NCO_LENGTH + 4));
+
+    if (baudrate) {
+        trace_ot_uart_check_baudrate(s->ot_id, s->pclk, baudrate);
+    }
+}
+
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+}
+
+static int ot_uart_can_receive(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
+    }
+
+    return 0;
+}
+
+static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    OtUARTState *s = opaque;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
+
+    if (size && !s->toggle_break) {
+        /* no longer breaking, so emulate idle in oversampled VAL register */
+        s->in_break = false;
+    }
+
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
+
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_event_handler(void *opaque, QEMUChrEvent event)
+{
+    OtUARTState *s = opaque;
+
+    if (event == CHR_EVENT_BREAK) {
+        if (!s->in_break || !s->oversample_break) {
+            /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+            s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+            ot_uart_update_irqs(s);
+            /* emulate break in the oversampled VAL register */
+            s->in_break = true;
+        } else if (s->toggle_break) {
+            /* emulate toggling break off in the oversampled VAL register */
+            s->in_break = false;
+        }
+    }
+}
+
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+}
+
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
+    }
+
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end of TX
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
+
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
+    }
+
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond,
+                                 void *opaque)
+{
+    OtUARTState *s = opaque;
+    (void)do_not_use;
+    (void)cond;
+
+    s->watch_tag = 0;
+    ot_uart_xmit(s);
+
+    return FALSE;
+}
+
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
+{
+    if (fifo8_is_full(&s->tx_fifo)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
+    }
+
+    fifo8_push(&s->tx_fifo, val);
+
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
+    }
+
+    if (ot_uart_is_tx_enabled(s)) {
+        ot_uart_xmit(s);
+    }
+}
+
+static void ot_uart_clock_input(void *opaque, int irq, int level)
+{
+    OtUARTState *s = opaque;
+
+    g_assert(irq == 0);
+
+    s->pclk = (unsigned)level;
+
+    /* TODO: disable UART transfer when PCLK is 0 */
+    ot_uart_check_baudrate(s);
+}
+
+static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;

Not sure this is required

hw/char/ot_uart.c new file +320 -0
+    uint32_t val32;
+
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
+    case R_INTR_STATE:
+    case R_INTR_ENABLE:
+    case R_CTRL:
+    case R_FIFO_CTRL:
+        val32 = s->regs[reg];
+        break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        val32 = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            val32 |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            val32 |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            val32 |= R_STATUS_TXIDLE_MASK;
+        }
+        if (!ot_uart_is_rx_enabled(s)) {
+            val32 |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
+    case R_RDATA:
+        val32 = (uint32_t)ot_uart_read_rx_fifo(s);
+        break;
+    case R_FIFO_STATUS:
+        val32 =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) << R_FIFO_STATUS_RXLVL_SHIFT;
+        val32 |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) << R_FIFO_STATUS_TXLVL_SHIFT;
+        break;
+    case R_VAL:
+        /*
+         * This is not trivially implemented due to the QEMU UART
+         * interface. There is no way to reliably sample or oversample
+         * given our emulated interface, but some software might poll the
+         * value of this register to determine break conditions.
+         *
+         * As such, default to reporting 16 of the last sample received
+         * instead. This defaults to 16 idle high samples (as a stop bit is
+         * always the last received), except for when the `oversample-break`
+         * property is set and a break condition is received over UART RX,
+         * where we then show 16 low samples until the next valid UART
+         * transmission is received (or break is toggled off with the
+         * `toggle-break` property enabled). This will not be accurate, but
+         * should be sufficient to support basic software flows that
+         * essentially use UART break as a strapping mechanism.
+         */
+        val32 = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX;
+        qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n", __func__,
+                      (s->oversample_break ? "/break" : ""));
+        break;
+    case R_OVRD:
+    case R_TIMEOUT_CTRL:
+        val32 = s->regs[reg];
+        qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__,
+                      REG_NAME(reg));
+        break;
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        val32 = 0;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        val32 = 0;
+        break;
+    }
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32,
+                              pc);
+
+    return (uint64_t)val32;
+}
+
+static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
+                          unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;
+    uint32_t val32 = val64;
+
+    hwaddr reg = R32_OFF(addr);
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32, pc);
+
+    switch (reg) {
+    case R_INTR_STATE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] &= ~val32; /* RW1C */
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_ENABLE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_ENABLE] = val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_TEST:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] |= val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_ALERT_TEST:
+        val32 &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = val32;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
+    case R_CTRL:
+        if (val32 & ~CTRL_SUP_MASK) {
+            qemu_log_mask(LOG_UNIMP,
+                          "%s: UART_CTRL feature not supported: 0x%08x\n",
+                          __func__, val32 & ~CTRL_SUP_MASK);
+        }
+        uint32_t prev = s->regs[R_CTRL];
+        s->regs[R_CTRL] = val32 & CTRL_MASK;
+        uint32_t change = prev ^ s->regs[R_CTRL];
+        if (change & R_CTRL_NCO_MASK) {
+            ot_uart_check_baudrate(s);
+        }
+        if ((change & R_CTRL_RX_MASK) && ot_uart_is_rx_enabled(s) &&
+            !ot_uart_is_sys_loopack_enabled(s)) {
+            qemu_chr_fe_accept_input(&s->chr);
+        }
+        if ((change & R_CTRL_TX_MASK) && ot_uart_is_tx_enabled(s)) {
+            /* try sending pending data from TX FIFO if any */
+            ot_uart_xmit(s);
+        }
+        break;
+    case R_WDATA:
+        uart_write_tx_fifo(s, (uint8_t)(val32 & R_WDATA_WDATA_MASK));
+        break;
+    case R_FIFO_CTRL:
+        s->regs[R_FIFO_CTRL] =
+            val32 & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
+        if (val32 & R_FIFO_CTRL_RXRST_MASK) {
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        if (val32 & R_FIFO_CTRL_TXRST_MASK) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        break;
+    case R_OVRD:
+        if (val32 & R_OVRD_TXEN_MASK) {
+            qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
+                          __func__);
+        }
+        s->regs[R_OVRD] = val32 & R_OVRD_TXVAL_MASK;
+        break;
+    case R_TIMEOUT_CTRL:
+        s->regs[R_TIMEOUT_CTRL] =
+            val32 & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
+        break;
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__,
+                      (uint32_t)addr);
+        break;
+    }
+}
+
+static const MemoryRegionOps ot_uart_ops = {
+    .read = ot_uart_read,
+    .write = ot_uart_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl.min_access_size = 4,
+    .impl.max_access_size = 4,
+};
+
+static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),
+    DEFINE_PROP_CHR("chardev", OtUARTState, chr),
+    DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
+    DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
+};
+
+static int ot_uart_be_change(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+
+    if (s->watch_tag > 0) {
+        g_source_remove(s->watch_tag);
+        /* NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) */
+        s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
+                                             ot_uart_watch_cb, s);
+    }
+
+    return 0;
+}
+
+static void ot_uart_reset_enter(Object *obj, ResetType type)
+{
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
+
+    memset(&s->regs[0], 0, sizeof(s->regs));
+
+    s->tx_watermark_level = 0;
+    for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
+        qemu_set_irq(s->irqs[index], 0);
+    }
+    ot_uart_reset_tx_fifo(s);
+    ot_uart_reset_rx_fifo(s);
+
+    /*
+     * do not reset `s->in_break`, as that tracks whether we are currently
+     * receiving a break condition over UART RX from some device talking
+     * to OpenTitan, which should survive resets. The QEMU CharDev only
+     * supports transient break events and not the notion of holding the
+     * UART in break, so remembering breaks like this is required to
+     * support mocking of break conditions in the oversampled `VAL` reg.
+     */
+    if (s->in_break) {
+        /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+        s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_realize(DeviceState *dev, Error **errp)
+{
+    OtUARTState *s = OT_UART(dev);
+    (void)errp;
+
+    g_assert(s->ot_id);
+
+    qdev_init_gpio_in_named(DEVICE(s), &ot_uart_clock_input, "clock-in", 1);
+
+    fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE);
+    fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE);
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive,
+                             ot_uart_event_handler, ot_uart_be_change, s, NULL,
+                             true);
+}
+
+static void ot_uart_init(Object *obj)
+{
+    OtUARTState *s = OT_UART(obj);
+
+    for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) {
+        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[index]);
+    }
+
+    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
+                          REGS_SIZE);
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+}
+
+static void ot_uart_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    (void)data;
+
+    dc->realize = ot_uart_realize;
+    device_class_set_props(dc, ot_uart_properties);
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
+}
+
+static const TypeInfo ot_uart_info = {
+    .name = TYPE_OT_UART,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(OtUARTState),
+    .instance_init = ot_uart_init,
+    .class_size = sizeof(OtUARTClass),
+    .class_init = ot_uart_class_init,
+};
+
+static void ot_uart_register_types(void)
+{
+    type_register_static(&ot_uart_info);
+}
+
+type_init(ot_uart_register_types);
hw/char/trace-events +8 -0
index a3fcc77287..c859d8af4e 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned pclk, unsigned baud) "%s: @ %u Hz: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +2 -1
index 309125e854..163d3ac3d3 100644
@@ -132,7 +132,8 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ibex_uart.h deleted +0 -73
deleted file mode 100644
index 882796e0c6..0000000000
@@ -1,73 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef HW_IBEX_UART_H
-#define HW_IBEX_UART_H
-
-#include "hw/core/sysbus.h"
-#include "chardev/char-fe.h"
-#include "qemu/timer.h"
-#include "qom/object.h"
-
-#define IBEX_UART_TX_FIFO_SIZE 16
-#define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
-
-#define TYPE_IBEX_UART "ibex-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
-
-struct IbexUartState {
-    /* <private> */
-    SysBusDevice parent_obj;
-
-    /* <public> */
-    MemoryRegion mmio;
-
-    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
-    uint32_t tx_level;
-
-    uint32_t rx_level;
-
-    QEMUTimer *fifo_trigger_handle;
-    uint64_t char_tx_time;
-
-    uint32_t uart_intr_state;
-    uint32_t uart_intr_enable;
-    uint32_t uart_ctrl;
-    uint32_t uart_status;
-    uint32_t uart_rdata;
-    uint32_t uart_fifo_ctrl;
-    uint32_t uart_fifo_status;
-    uint32_t uart_ovrd;
-    uint32_t uart_val;
-    uint32_t uart_timeout_ctrl;
-
-    Clock *f_clk;
-
-    CharFrontend chr;
-    qemu_irq tx_watermark;
-    qemu_irq rx_watermark;
-    qemu_irq tx_empty;
-    qemu_irq rx_overflow;
-};
-#endif /* HW_IBEX_UART_H */
include/hw/char/ot_uart.h new file +77 -0
new file mode 100644
index 0000000000..45ec3fc7c2
@@ -0,0 +1,133 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ */
+
+#ifndef HW_OPENTITAN_OT_UART_H
+#define HW_OPENTITAN_OT_UART_H
+
+#include "qemu/fifo8.h"
+#include "chardev/char-fe.h"
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+#include "hw/core/registerfields.h"
+
+#define TYPE_OT_UART "ot-uart"
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
+
+/* clang-format off */
+REG32(INTR_STATE, 0x00u)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0u, 1u)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1u, 1u)
+    SHARED_FIELD(INTR_TX_DONE, 2u, 1u)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3u, 1u)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4u, 1u)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5u, 1u)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6u, 1u)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7u, 1u)
+    SHARED_FIELD(INTR_TX_EMPTY, 8u, 1u)
+REG32(INTR_ENABLE, 0x04u)
+REG32(INTR_TEST, 0x08u)
+REG32(ALERT_TEST, 0x0cu)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u)
+REG32(CTRL, 0x10u)
+    FIELD(CTRL, TX, 0u, 1u)
+    FIELD(CTRL, RX, 1u, 1u)
+    FIELD(CTRL, NF, 2u, 1u)
+    FIELD(CTRL, SLPBK, 4u, 1u)
+    FIELD(CTRL, LLPBK, 5u, 1u)
+    FIELD(CTRL, PARITY_EN, 6u, 1u)
+    FIELD(CTRL, PARITY_ODD, 7u, 1u)
+    FIELD(CTRL, RXBLVL, 8u, 2u)
+    FIELD(CTRL, NCO, 16u, 16u)
+REG32(STATUS, 0x14u)
+    FIELD(STATUS, TXFULL, 0u, 1u)
+    FIELD(STATUS, RXFULL, 1u, 1u)
+    FIELD(STATUS, TXEMPTY, 2u, 1u)
+    FIELD(STATUS, TXIDLE, 3u, 1u)
+    FIELD(STATUS, RXIDLE, 4u, 1u)
+    FIELD(STATUS, RXEMPTY, 5u, 1u)
+REG32(RDATA, 0x18u)
+    FIELD(RDATA, RDATA, 0u, 8u)
+REG32(WDATA, 0x1cu)
+    FIELD(WDATA, WDATA, 0u, 8u)
+REG32(FIFO_CTRL, 0x20u)
+    FIELD(FIFO_CTRL, RXRST, 0u, 1u)
+    FIELD(FIFO_CTRL, TXRST, 1u, 1u)
+    FIELD(FIFO_CTRL, RXILVL, 2u, 3u)
+    FIELD(FIFO_CTRL, TXILVL, 5u, 3u)
+REG32(FIFO_STATUS, 0x24u)
+    FIELD(FIFO_STATUS, TXLVL, 0u, 8u)
+    FIELD(FIFO_STATUS, RXLVL, 16u, 8u)
+REG32(OVRD, 0x28u)
+    FIELD(OVRD, TXEN, 0u, 1u)
+    FIELD(OVRD, TXVAL, 1u, 1u)
+REG32(VAL, 0x2cu)
+    FIELD(VAL, RX, 0u, 16u)
+REG32(TIMEOUT_CTRL, 0x30u)
+    FIELD(TIMEOUT_CTRL, VAL, 0u, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31u, 1u)
+/* clang-format on */
Quoted 3 lines

These shouldn't need to be public

agreed. They are here because the struct definition below uses REGS_COUNT which is computed based on these definitions. I can hard-code the length of 13 instead? It looks like other hardware blocks also hard-code this, is there some way to at least catch the error if this stops being self-consistent?

Yeah, everyone else just hard codes a length or defines each register as a u32/u64.

Good question about an assert. You could do a runtime assert in the init() function. The BSD user headers also use _Static_assert() [1], so that should be an option. I haven't checked that all supported compilers support _Static_assert though

1: https://en.cppreference.com/w/c/language/_Static_assert.html

Alistair

Quoted 87 lines

Alistair

include/hw/char/ot_uart.h new file +56 -0
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define CTRL_SUP_MASK \
+    (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16u
+#define OT_UART_TX_FIFO_SIZE 128u
+#define OT_UART_RX_FIFO_SIZE 128u
+#define OT_UART_IRQ_NUM      9u
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?")
+
+struct OtUARTState {
+    SysBusDevice parent_obj;
+    MemoryRegion mmio;
+    qemu_irq irqs[OT_UART_IRQ_NUM];
+
+    uint32_t regs[REGS_COUNT];
+
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+    uint32_t tx_watermark_level;
+    bool in_break;
+    guint watch_tag;
+    unsigned pclk; /* Current input clock */
+    const char *clock_src_name; /* IRQ name once connected */
+
+    char *ot_id;
+    char *clock_name;
+    DeviceState *clock_src;
+    CharFrontend chr;
+    bool oversample_break; /* Should mock break in the oversampled VAL reg? */
+    bool toggle_break; /* Are incoming breaks temporary or toggled? */
+};
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
+#endif /* HW_OPENTITAN_OT_UART_H */
include/hw/riscv/opentitan.h +2 -3
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
 #include "hw/timer/ibex_timer.h"
 #include "hw/ssi/ibex_spi_host.h"
 #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
     IbexTimerState timer;
     IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
--

2.49.1

--
Lex Bailey (they/them)
lowRISC C.I.C. 7 Hills Road, Cambridge, CB2 1GE, UK. Registered in England & Wales 09272283 [email protected]

#5 lore raw

Re: [PATCH qemu 1/1] Replace opentitan UART device with newer version (opentitan 1.0)

Estimado Alistair:

Muchas gracias por su revisión detallada. Estoy de acuerdo con sus observaciones y procederé de la siguiente manera:

En primer lugar, realizaré el renombre del archivo de ibex_uart a ot_uart en un commit separado con su debida justificación, para luego incluir los cambios de implementación en commits posteriores.

Asimismo, me aseguraré de utilizar las macros de QEMU existentes, como FIELD_EX32, en lugar de definiciones personalizadas. También corregiré el uso de los sufijos, los repartos de tipos y la visibilidad de las definiciones según lo sugerido. Respecto a la consistencia de la longitud del registro, evaluaré la implementación de una aserción estática o en tiempo de ejecución.

Agradezco mucho su tiempo y sus comentarios.

Atentamente,

Jose Martinez

Quoted 1,688 lines · Alistair Francis
Quoted 42 lines · Lex Bailey

On 26/03/2026 02:39, Alistair Francis wrote:

On Fri, Mar 20, 2026 at 12:50 AM ~lexbaileylowrisc <[email protected]> wrote:

You need a commit message, it should describe what you are doing, link to specs and provide details for future readers.

From: Lex Bailey <[email protected]>

---
 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
hw/char/ibex_uart.c deleted +0 -0
deleted file mode 100644
index 127d219df3..0000000000

I don't think we should remove the file. If you want to rename it that's fine, but that should be its own commit. So one commit to rename it (with a justification in the commit message) and then more commits to make changes.

okay, I will rename it first and then make changes

hw/char/ibex_uart.c line 9

Quoted 9 lines
hw/char/ibex_uart.c deleted +0 -9
@@ -1,569 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * For details check the documentation here:
- *    https://docs.opentitan.org/hw/ip/uart/doc/
- *
- * Permission is hereby granted, free of charge, to any person

obtaining a copy

hw/char/ibex_uart.c line 10

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * of this software and associated documentation files (the

"Software"), to deal

hw/char/ibex_uart.c line 11

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * in the Software without restriction, including without limitation

the rights

hw/char/ibex_uart.c line 12

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or

sell

hw/char/ibex_uart.c

Quoted 4 lines
hw/char/ibex_uart.c deleted +0 -4
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be

included in

hw/char/ibex_uart.c

Quoted 3 lines
hw/char/ibex_uart.c deleted +0 -3
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS OR

hw/char/ibex_uart.c line 20

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

MERCHANTABILITY,

hw/char/ibex_uart.c line 21

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT

SHALL

hw/char/ibex_uart.c line 22

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

OTHER

hw/char/ibex_uart.c line 23

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

ARISING FROM,

hw/char/ibex_uart.c line 24

Quoted 1 line
hw/char/ibex_uart.c deleted +0 -1
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER

DEALINGS IN

Quoted 45 lines
hw/char/ibex_uart.c deleted +0 -45
- * THE SOFTWARE.
- */
-
-#include "qemu/osdep.h"
-#include "hw/char/ibex_uart.h"
-#include "hw/core/irq.h"
-#include "hw/core/qdev-clock.h"
-#include "hw/core/qdev-properties.h"
-#include "hw/core/qdev-properties-system.h"
-#include "hw/core/registerfields.h"
-#include "migration/vmstate.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
-
-REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
-REG32(INTR_ENABLE, 0x04)
-REG32(INTR_TEST, 0x08)
-REG32(ALERT_TEST, 0x0C)
-REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
-    FIELD(CTRL, NF, 2, 1)
-    FIELD(CTRL, SLPBK, 4, 1)
-    FIELD(CTRL, LLPBK, 5, 1)
-    FIELD(CTRL, PARITY_EN, 6, 1)
-    FIELD(CTRL, PARITY_ODD, 7, 1)
-    FIELD(CTRL, RXBLVL, 8, 2)
-    FIELD(CTRL, NCO, 16, 16)
-REG32(STATUS, 0x14)
-    FIELD(STATUS, TXFULL, 0, 1)
-    FIELD(STATUS, RXFULL, 1, 1)
-    FIELD(STATUS, TXEMPTY, 2, 1)
-    FIELD(STATUS, RXIDLE, 4, 1)
-    FIELD(STATUS, RXEMPTY, 5, 1)
-REG32(RDATA, 0x18)
-REG32(WDATA, 0x1C)
-REG32(FIFO_CTRL, 0x20)
-    FIELD(FIFO_CTRL, RXRST, 0, 1)
-    FIELD(FIFO_CTRL, TXRST, 1, 1)
-    FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
-REG32(OVRD, 0x28)
-REG32(VAL, 0x2C)
-REG32(TIMEOUT_CTRL, 0x30)
-
-static void ibex_uart_update_irqs(IbexUartState *s)
-{
-    if (s->uart_intr_state & s->uart_intr_enable &

R_INTR_STATE_TX_WATERMARK_MASK) {

hw/char/ibex_uart.c

Quoted 6 lines
hw/char/ibex_uart.c deleted +0 -6
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable &

R_INTR_STATE_RX_WATERMARK_MASK) {

hw/char/ibex_uart.c

Quoted 6 lines
hw/char/ibex_uart.c deleted +0 -6
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable &

R_INTR_STATE_TX_EMPTY_MASK) {

hw/char/ibex_uart.c

Quoted 6 lines
hw/char/ibex_uart.c deleted +0 -6
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->uart_intr_state & s->uart_intr_enable &

R_INTR_STATE_RX_OVERFLOW_MASK) {

Quoted 9 lines
hw/char/ibex_uart.c deleted +0 -9
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
-    }
-}
-
-static int ibex_uart_can_receive(void *opaque)
-{
-    IbexUartState *s = opaque;

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
-    }
-
-    return 0;
-}
-
-static void ibex_uart_receive(void *opaque, const uint8_t *buf, int

size)

hw/char/ibex_uart.c

Quoted 3 lines
hw/char/ibex_uart.c deleted +0 -3
-{
-    IbexUartState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl &

R_FIFO_CTRL_RXILVL_MASK)

Quoted 14 lines
hw/char/ibex_uart.c deleted +0 -14
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
-
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
-
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-    }
-
-    ibex_uart_update_irqs(s);
-}
-
-static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
-                               void *opaque)
-{
-    IbexUartState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl &

R_FIFO_CTRL_TXILVL_MASK)

Quoted 49 lines
hw/char/ibex_uart.c deleted +0 -49
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
-    }
-
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
-    }
-
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
-
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
-
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ibex_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
-    }
-
-    /* Clear the TX Full bit */
-    if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-    }
-
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-    }
-
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-    }

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-
-    ibex_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
-}
-
-static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
-                               int size)
-{
-    uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl &

R_FIFO_CTRL_TXILVL_MASK)

Quoted 80 lines
hw/char/ibex_uart.c deleted +0 -80
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
-        qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
-    }
-
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
-
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
-    }
-
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ibex_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
-    }
-
-    timer_mod(s->fifo_trigger_handle, current_time +
-              (s->char_tx_time * 4));
-}
-
-static void ibex_uart_reset(DeviceState *dev)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
-
-    s->tx_level = 0;
-    s->rx_level = 0;
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
-
-    ibex_uart_update_irqs(s);
-}
-
-static uint64_t ibex_uart_get_baud(IbexUartState *s)
-{
-    uint64_t baud;
-
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
-    baud *= clock_get_hz(s->f_clk);
-    baud >>= 20;
-
-    return baud;
-}
-
-static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
-                                       unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint64_t retvalue = 0;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
-        break;
-    case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-    case R_CTRL:
-        retvalue = s->uart_ctrl;
-        break;
-    case R_STATUS:
-        retvalue = s->uart_status;
-        break;
-
-    case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level >

0)) {

Quoted 36 lines
hw/char/ibex_uart.c deleted +0 -36
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
-        break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
-    case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
-        break;
-    case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
-        break;
-
-    case R_OVRD:
-        retvalue = s->uart_ovrd;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        retvalue = s->uart_val;
-        qemu_log_mask(LOG_UNIMP,

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-                      "%s: val is not supported\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__,

addr);

Quoted 23 lines
hw/char/ibex_uart.c deleted +0 -23
-        return 0;
-    }
-
-    return retvalue;
-}
-
-static void ibex_uart_write(void *opaque, hwaddr addr,
-                                  uint64_t val64, unsigned int size)
-{
-    IbexUartState *s = opaque;
-    uint32_t value = val64;
-
-    switch (addr >> 2) {
-    case R_INTR_STATE:
-        /* Write 1 clear */
-        s->uart_intr_state &= ~value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
-        ibex_uart_update_irqs(s);
-        break;
-    case R_INTR_TEST:

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-        s->uart_intr_state |= value;
-        ibex_uart_update_irqs(s);
-        break;
-
-    case R_CTRL:
-        s->uart_ctrl = value;
-
-        if (value & R_CTRL_NF_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_NF is not supported\n",

__func__);

hw/char/ibex_uart.c

Quoted 4 lines
hw/char/ibex_uart.c deleted +0 -4
-        }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n",

__func__);

hw/char/ibex_uart.c

Quoted 4 lines
hw/char/ibex_uart.c deleted +0 -4
-        }
-        if (value & R_CTRL_LLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_LLPBK is not supported\n",

__func__);

Quoted 4 lines
hw/char/ibex_uart.c deleted +0 -4
-        }
-        if (value & R_CTRL_PARITY_EN_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_EN is not supported\n",

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-                          __func__);
-        }
-        if (value & R_CTRL_PARITY_ODD_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_PARITY_ODD is not supported\n",
-                          __func__);
-        }
-        if (value & R_CTRL_RXBLVL_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_RXBLVL is not supported\n",

__func__);

Quoted 44 lines
hw/char/ibex_uart.c deleted +0 -44
-        }
-        if (value & R_CTRL_NCO_MASK) {
-            uint64_t baud = ibex_uart_get_baud(s);
-
-            s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-        }
-        break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
-    case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
-        break;
-
-    case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
-
-        if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
-        }
-        if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
-        }
-        break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
-
-    case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,

hw/char/ibex_uart.c

Quoted 10 lines
hw/char/ibex_uart.c deleted +0 -10
-                      "%s: val is read only\n", __func__);
-        break;
-    case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
-        break;
-    default:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__,

addr);

hw/char/meson.build system_ss.add(when: 'CONFIG_CADENCE', if_true:

Quoted 127 lines
hw/char/ibex_uart.c deleted +0 -122
-    }
-}
-
-static void ibex_uart_clk_update(void *opaque, ClockEvent event)
-{
-    IbexUartState *s = opaque;
-
-    /* recompute uart's speed on clock change */
-    uint64_t baud = ibex_uart_get_baud(s);
-
-    s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
-}
-
-static void fifo_trigger_update(void *opaque)
-{
-    IbexUartState *s = opaque;
-
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ibex_uart_xmit(NULL, G_IO_OUT, s);
-    }
-}
-
-static const MemoryRegionOps ibex_uart_ops = {
-    .read = ibex_uart_read,
-    .write = ibex_uart_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
-    .impl.min_access_size = 4,
-    .impl.max_access_size = 4,
-};
-
-static int ibex_uart_post_load(void *opaque, int version_id)
-{
-    IbexUartState *s = opaque;
-
-    ibex_uart_update_irqs(s);
-    return 0;
-}
-
-static const VMStateDescription vmstate_ibex_uart = {
-    .name = TYPE_IBEX_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
-    .post_load = ibex_uart_post_load,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
-                            IBEX_UART_TX_FIFO_SIZE),
-        VMSTATE_UINT32(tx_level, IbexUartState),
-        VMSTATE_UINT64(char_tx_time, IbexUartState),
-        VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
-        VMSTATE_UINT32(uart_intr_state, IbexUartState),
-        VMSTATE_UINT32(uart_intr_enable, IbexUartState),
-        VMSTATE_UINT32(uart_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_status, IbexUartState),
-        VMSTATE_UINT32(uart_rdata, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
-        VMSTATE_UINT32(uart_fifo_status, IbexUartState),
-        VMSTATE_UINT32(uart_ovrd, IbexUartState),
-        VMSTATE_UINT32(uart_val, IbexUartState),
-        VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const Property ibex_uart_properties[] = {
-    DEFINE_PROP_CHR("chardev", IbexUartState, chr),
-};
-
-static void ibex_uart_init(Object *obj)
-{
-    IbexUartState *s = IBEX_UART(obj);
-
-    s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
-                                  ibex_uart_clk_update, s, ClockUpdate);
-    clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
-
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
-
-    memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
-                          TYPE_IBEX_UART, 0x400);
-    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
-}
-
-static void ibex_uart_realize(DeviceState *dev, Error **errp)
-{
-    IbexUartState *s = IBEX_UART(dev);
-
-    s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                          fifo_trigger_update, s);
-
-    qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
-                             ibex_uart_receive, NULL, NULL,
-                             s, NULL, true);
-}
-
-static void ibex_uart_class_init(ObjectClass *klass, const void *data)
-{
-    DeviceClass *dc = DEVICE_CLASS(klass);
-
-    device_class_set_legacy_reset(dc, ibex_uart_reset);
-    dc->realize = ibex_uart_realize;
-    dc->vmsd = &vmstate_ibex_uart;
-    device_class_set_props(dc, ibex_uart_properties);
-    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
-}
-
-static const TypeInfo ibex_uart_info = {
-    .name          = TYPE_IBEX_UART,
-    .parent        = TYPE_SYS_BUS_DEVICE,
-    .instance_size = sizeof(IbexUartState),
-    .instance_init = ibex_uart_init,
-    .class_init    = ibex_uart_class_init,
-};
-
-static void ibex_uart_register_types(void)
-{
-    type_register_static(&ibex_uart_info);
-}
-
-type_init(ibex_uart_register_types)
hw/char/meson.build +0 -0
index fc3d7ee506..989ca3d7e0 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true:

files('cadence_uart.c'))

hw/char/meson.build system_ss.add(when: 'CONFIG_CADENCE', if_true: · line 2

Quoted 1 line
hw/char/meson.build +0 -0
 system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true:

files('cmsdk-apb-uart.c'))

hw/char/meson.build system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))

Quoted 6 lines
hw/char/meson.build +1 -1
 system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
 system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_IBEX', if_true: files('ot_uart.c'))
 system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
 system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true:

files('ipoctal232.c'))

Quoted 40 lines
hw/char/meson.build +0 -0
 system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c new file +32 -0
new file mode 100644
index 0000000000..757f64ab70
@@ -0,0 +1,635 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025 lowRISC contributors.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Based on original ibex_uart implementation:
+ *  Copyright (c) 2020 Western Digital
+ *  Alistair Francis <[email protected]>
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/fifo8.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+#include "chardev/char-fe.h"
+#include "hw/core/cpu.h"
+#include "hw/core/irq.h"
+#include "hw/char/ot_uart.h"
+#include "hw/core/qdev-properties-system.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/registerfields.h"
+#include "trace.h"
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)
Quoted 3 lines

Let's use the existing QEMU macros instead of creating custom ones.

Specifically which macro should I be using instead? I can't find any

that do the same as this one. It is used to populate the array of register names just below, which are used in log/trace messages

Ah sorry. I originally thought this was instead of the REG32 macros, hence the comment. Then forgot to come back and delete the comment before sending.

Quoted 24 lines
hw/char/ot_uart.c new file +22 -0
+static const char *REG_NAMES[REGS_COUNT] = {
+    /* clang-format off */
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+    /* clang-format on */
+};
+#undef REG_NAME_ENTRY
+
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_TXILVL_MASK)
Quoted 1 line
Quoted 3 lines

+ R_FIFO_CTRL_TXILVL_SHIFT;

You can then use the extract (FIELD_EX32) macros instead of mask and

shift.

Quoted 12 lines
hw/char/ot_uart.c new file +2 -0
+
+    return tx_ilvl < 7u ? (1u << tx_ilvl) : 64u;

You shouldn't need the us

hw/char/ot_uart.c new file +5 -0
+}
+
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = (s->regs[R_FIFO_CTRL] & R_FIFO_CTRL_RXILVL_MASK)
Quoted 1 line

hw/char/ot_uart.c

Quoted 8 lines
hw/char/ot_uart.c new file +8 -0
+                       R_FIFO_CTRL_RXILVL_SHIFT;
+
+    return rx_ilvl < 7u ? (1u << rx_ilvl) : 126u;
+}
+
+static void ot_uart_update_irqs(OtUARTState *s)
+{
+    uint32_t state_masked = s->regs[R_INTR_STATE] &

s->regs[R_INTR_ENABLE];

hw/char/ot_uart.c

Quoted 2 lines
hw/char/ot_uart.c new file +2 -0
+
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE],

s->regs[R_INTR_ENABLE],

Quoted 35 lines
hw/char/ot_uart.c new file +11 -0
+                       state_masked);
+
+    for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
+        bool level = (state_masked & (1U << index)) != 0;
+        qemu_set_irq(s->irqs[index], level);
+    }
+}
+
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);

You also should need to cast this

hw/char/ot_uart.c new file +17 -0
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
+static void ot_uart_check_baudrate(const OtUARTState *s)
+{
+    uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO);
+
+    unsigned baudrate = (unsigned)(((uint64_t)nco * (uint64_t)s->pclk)
Quoted 1 line
hw/char/ot_uart.c new file +3 -0
+                                   (R_CTRL_NCO_LENGTH + 4));
+
+    if (baudrate) {

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        trace_ot_uart_check_baudrate(s->ot_id, s->pclk, baudrate);
+    }
+}
+
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s))

{

Quoted 13 lines
hw/char/ot_uart.c new file +13 -0
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+}
+
+static int ot_uart_can_receive(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
+    }
+
+    return 0;

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+}
+
+static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
+{
+    OtUARTState *s = opaque;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
+
+    if (size && !s->toggle_break) {
+        /* no longer breaking, so emulate idle in oversampled VAL

register */

Quoted 16 lines
hw/char/ot_uart.c new file +16 -0
+        s->in_break = false;
+    }
+
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
+
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
+    }
+

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    ot_uart_update_irqs(s);
+}
+
+static void ot_uart_event_handler(void *opaque, QEMUChrEvent event)
+{
+    OtUARTState *s = opaque;
+
+    if (event == CHR_EVENT_BREAK) {
+        if (!s->in_break || !s->oversample_break) {
+            /* ignore CTRL.RXBLVL as we have no notion of break "time"

*/

hw/char/ot_uart.c

Quoted 6 lines
hw/char/ot_uart.c new file +6 -0
+            s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+            ot_uart_update_irqs(s);
+            /* emulate break in the oversampled VAL register */
+            s->in_break = true;
+        } else if (s->toggle_break) {
+            /* emulate toggling break off in the oversampled VAL

register */

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+            s->in_break = false;
+        }
+    }
+}
+
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s))

{

Quoted 25 lines
hw/char/ot_uart.c new file +25 -0
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+}
+
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    }
+
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end

of TX

Quoted 7 lines
hw/char/ot_uart.c new file +7 -0
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
+
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo),

&size);

Quoted 38 lines
hw/char/ot_uart.c new file +38 -0
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
+    }
+
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
+    }
+
+    ot_uart_update_irqs(s);
+}
+
+static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond,
+                                 void *opaque)
+{
+    OtUARTState *s = opaque;
+    (void)do_not_use;
+    (void)cond;
+
+    s->watch_tag = 0;
+    ot_uart_xmit(s);
+
+    return FALSE;
+}
+
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
+{
+    if (fifo8_is_full(&s->tx_fifo)) {

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
+    }
+
+    fifo8_push(&s->tx_fifo, val);
+
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from

above

Quoted 1 line

+ * watermark to below. If we haven't reached watermark, reset

cached

Quoted 67 lines
hw/char/ot_uart.c new file +26 -0
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
+    }
+
+    if (ot_uart_is_tx_enabled(s)) {
+        ot_uart_xmit(s);
+    }
+}
+
+static void ot_uart_clock_input(void *opaque, int irq, int level)
+{
+    OtUARTState *s = opaque;
+
+    g_assert(irq == 0);
+
+    s->pclk = (unsigned)level;
+
+    /* TODO: disable UART transfer when PCLK is 0 */
+    ot_uart_check_baudrate(s);
+}
+
+static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;

Not sure this is required

hw/char/ot_uart.c new file +38 -0
+    uint32_t val32;
+
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
+    case R_INTR_STATE:
+    case R_INTR_ENABLE:
+    case R_CTRL:
+    case R_FIFO_CTRL:
+        val32 = s->regs[reg];
+        break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        val32 = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            val32 |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            val32 |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            val32 |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            val32 |= R_STATUS_TXIDLE_MASK;
+        }

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        if (!ot_uart_is_rx_enabled(s)) {
+            val32 |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
+    case R_RDATA:
+        val32 = (uint32_t)ot_uart_read_rx_fifo(s);
+        break;
+    case R_FIFO_STATUS:
+        val32 =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) <<

R_FIFO_STATUS_RXLVL_SHIFT;

hw/char/ot_uart.c

Quoted 2 lines
hw/char/ot_uart.c new file +2 -0
+        val32 |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) <<

R_FIFO_STATUS_TXLVL_SHIFT;

hw/char/ot_uart.c

Quoted 6 lines
hw/char/ot_uart.c new file +6 -0
+        break;
+    case R_VAL:
+        /*
+         * This is not trivially implemented due to the QEMU UART
+         * interface. There is no way to reliably sample or oversample
+         * given our emulated interface, but some software might poll

the

hw/char/ot_uart.c

Quoted 4 lines
hw/char/ot_uart.c new file +4 -0
+         * value of this register to determine break conditions.
+         *
+         * As such, default to reporting 16 of the last sample received
+         * instead. This defaults to 16 idle high samples (as a stop

bit is

Quoted 1 line

+ * always the last received), except for when the

oversample-break

Quoted 1 line

+ * property is set and a break condition is received over UART

RX,

hw/char/ot_uart.c

Quoted 3 lines
hw/char/ot_uart.c new file +3 -0
+         * where we then show 16 low samples until the next valid UART
+         * transmission is received (or break is toggled off with the
+         * `toggle-break` property enabled). This will not be accurate,

but

hw/char/ot_uart.c

Quoted 5 lines
hw/char/ot_uart.c new file +5 -0
+         * should be sufficient to support basic software flows that
+         * essentially use UART break as a strapping mechanism.
+         */
+        val32 = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX;
+        qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n",

__func__,

Quoted 7 lines
hw/char/ot_uart.c new file +7 -0
+                      (s->oversample_break ? "/break" : ""));
+        break;
+    case R_OVRD:
+    case R_TIMEOUT_CTRL:
+        val32 = s->regs[reg];
+        qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__,
+                      REG_NAME(reg));

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        break;
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        val32 = 0;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n",

__func__,

hw/char/ot_uart.c

Quoted 7 lines
hw/char/ot_uart.c new file +7 -0
+                      (uint32_t)addr);
+        val32 = 0;
+        break;
+    }
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg),

val32,

Quoted 6 lines
hw/char/ot_uart.c new file +6 -0
+                              pc);
+
+    return (uint64_t)val32;
+}
+
+static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+                          unsigned size)
+{
+    OtUARTState *s = opaque;
+    (void)size;
+    uint32_t val32 = val64;
+
+    hwaddr reg = R32_OFF(addr);
+
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg),

val32, pc);

Quoted 16 lines
hw/char/ot_uart.c new file +16 -0
+
+    switch (reg) {
+    case R_INTR_STATE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] &= ~val32; /* RW1C */
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_ENABLE:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_ENABLE] = val32;
+        ot_uart_update_irqs(s);
+        break;
+    case R_INTR_TEST:
+        val32 &= INTR_MASK;
+        s->regs[R_INTR_STATE] |= val32;
+        ot_uart_update_irqs(s);

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        break;
+    case R_ALERT_TEST:
+        val32 &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = val32;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
+    case R_CTRL:
+        if (val32 & ~CTRL_SUP_MASK) {
+            qemu_log_mask(LOG_UNIMP,
+                          "%s: UART_CTRL feature not supported:

0x%08x\n",

Quoted 42 lines
hw/char/ot_uart.c new file +42 -0
+                          __func__, val32 & ~CTRL_SUP_MASK);
+        }
+        uint32_t prev = s->regs[R_CTRL];
+        s->regs[R_CTRL] = val32 & CTRL_MASK;
+        uint32_t change = prev ^ s->regs[R_CTRL];
+        if (change & R_CTRL_NCO_MASK) {
+            ot_uart_check_baudrate(s);
+        }
+        if ((change & R_CTRL_RX_MASK) && ot_uart_is_rx_enabled(s) &&
+            !ot_uart_is_sys_loopack_enabled(s)) {
+            qemu_chr_fe_accept_input(&s->chr);
+        }
+        if ((change & R_CTRL_TX_MASK) && ot_uart_is_tx_enabled(s)) {
+            /* try sending pending data from TX FIFO if any */
+            ot_uart_xmit(s);
+        }
+        break;
+    case R_WDATA:
+        uart_write_tx_fifo(s, (uint8_t)(val32 & R_WDATA_WDATA_MASK));
+        break;
+    case R_FIFO_CTRL:
+        s->regs[R_FIFO_CTRL] =
+            val32 & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
+        if (val32 & R_FIFO_CTRL_RXRST_MASK) {
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        if (val32 & R_FIFO_CTRL_TXRST_MASK) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+        }
+        break;
+    case R_OVRD:
+        if (val32 & R_OVRD_TXEN_MASK) {
+            qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
+                          __func__);
+        }
+        s->regs[R_OVRD] = val32 & R_OVRD_TXVAL_MASK;
+        break;
+    case R_TIMEOUT_CTRL:
+        s->regs[R_TIMEOUT_CTRL] =
+            val32 & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+        break;
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n",
+                      __func__, (uint32_t)addr, REG_NAME(reg));
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n",

__func__,

Quoted 7 lines
hw/char/ot_uart.c new file +7 -0
+                      (uint32_t)addr);
+        break;
+    }
+}
+
+static const MemoryRegionOps ot_uart_ops = {
+    .read = ot_uart_read,

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    .write = ot_uart_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl.min_access_size = 4,
+    .impl.max_access_size = 4,
+};
+
+static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),
+    DEFINE_PROP_CHR("chardev", OtUARTState, chr),
+    DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break,

false),

hw/char/ot_uart.c

Quoted 8 lines
hw/char/ot_uart.c new file +8 -0
+    DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
+};
+
+static int ot_uart_be_change(void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive,

ot_uart_receive,

Quoted 1 line

+ ot_uart_event_handler, ot_uart_be_change,

s, NULL,

hw/char/ot_uart.c

Quoted 5 lines
hw/char/ot_uart.c new file +5 -0
+                             true);
+
+    if (s->watch_tag > 0) {
+        g_source_remove(s->watch_tag);
+        /* NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)

*/

Quoted 1 line

+ s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT |

G_IO_HUP,

Quoted 16 lines
hw/char/ot_uart.c new file +16 -0
+                                             ot_uart_watch_cb, s);
+    }
+
+    return 0;
+}
+
+static void ot_uart_reset_enter(Object *obj, ResetType type)
+{
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
+
+    memset(&s->regs[0], 0, sizeof(s->regs));

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+
+    s->tx_watermark_level = 0;
+    for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
+        qemu_set_irq(s->irqs[index], 0);
+    }
+    ot_uart_reset_tx_fifo(s);
+    ot_uart_reset_rx_fifo(s);
+
+    /*
+     * do not reset `s->in_break`, as that tracks whether we are

currently

Quoted 12 lines
hw/char/ot_uart.c new file +12 -0
+     * receiving a break condition over UART RX from some device talking
+     * to OpenTitan, which should survive resets. The QEMU CharDev only
+     * supports transient break events and not the notion of holding the
+     * UART in break, so remembering breaks like this is required to
+     * support mocking of break conditions in the oversampled `VAL` reg.
+     */
+    if (s->in_break) {
+        /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+        s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+    }
+
+    ot_uart_update_irqs(s);

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+}
+
+static void ot_uart_realize(DeviceState *dev, Error **errp)
+{
+    OtUARTState *s = OT_UART(dev);
+    (void)errp;
+
+    g_assert(s->ot_id);
+
+    qdev_init_gpio_in_named(DEVICE(s), &ot_uart_clock_input,

"clock-in", 1);

hw/char/ot_uart.c

Quoted 5 lines
hw/char/ot_uart.c new file +5 -0
+
+    fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE);
+    fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE);
+
+    qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive,

ot_uart_receive,

Quoted 1 line

+ ot_uart_event_handler, ot_uart_be_change,

s, NULL,

Quoted 18 lines
hw/char/ot_uart.c new file +18 -0
+                             true);
+}
+
+static void ot_uart_init(Object *obj)
+{
+    OtUARTState *s = OT_UART(obj);
+
+    for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) {
+        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[index]);
+    }
+
+    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
+                          REGS_SIZE);
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+}
+
+static void ot_uart_class_init(ObjectClass *klass, const void *data)
+{

hw/char/ot_uart.c

Quoted 10 lines
hw/char/ot_uart.c new file +10 -0
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    (void)data;
+
+    dc->realize = ot_uart_realize;
+    device_class_set_props(dc, ot_uart_properties);
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL,

NULL,

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s

Quoted 23 lines
hw/char/ot_uart.c new file +18 -0
+                                       &uc->parent_phases);
+}
+
+static const TypeInfo ot_uart_info = {
+    .name = TYPE_OT_UART,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(OtUARTState),
+    .instance_init = ot_uart_init,
+    .class_size = sizeof(OtUARTClass),
+    .class_init = ot_uart_class_init,
+};
+
+static void ot_uart_register_types(void)
+{
+    type_register_static(&ot_uart_info);
+}
+
+type_init(ot_uart_register_types);
hw/char/trace-events +0 -0
index a3fcc77287..c859d8af4e 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s

receiving '%c'"

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 142

Quoted 2 lines
hw/char/trace-events +0 -0
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload)

"device: %u cmd: %02u payload: %016" PRIx64

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 143

Quoted 1 line
hw/char/trace-events +0 -0
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t

payload) "device: %u cmd: %02u payload: %016" PRIx64

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"

Quoted 3 lines
hw/char/trace-events +3 -0
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned pclk, unsigned baud)

"%s: @ %u Hz: %u bps"

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 147

Quoted 1 line
hw/char/trace-events +1 -0
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s:

%s"

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 149

Quoted 2 lines
hw/char/trace-events +2 -0
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname,

uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 150

Quoted 1 line
hw/char/trace-events +1 -0
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname,

uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"

hw/char/trace-events stm32f2xx_usart_receive(char *id, uint8_t chr) " %s · line 151

Quoted 1 line
hw/char/trace-events +1 -0
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t

eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"

hw/riscv/opentitan.c static void lowrisc_ibex_soc_init(Object *obj) · line 136

Quoted 11 lines
hw/riscv/opentitan.c +2 -1
index 309125e854..163d3ac3d3 100644
@@ -132,7 +132,8 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0",

&error_fatal);

Quoted 9 lines
hw/riscv/opentitan.c +0 -0
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ibex_uart.h deleted +0 -0
deleted file mode 100644
index 882796e0c6..0000000000

include/hw/char/ibex_uart.h line 6

Quoted 6 lines
include/hw/char/ibex_uart.h deleted +0 -6
@@ -1,73 +0,0 @@
-/*
- * QEMU lowRISC Ibex UART device
- *
- * Copyright (c) 2020 Western Digital
- *
- * Permission is hereby granted, free of charge, to any person

obtaining a copy

include/hw/char/ibex_uart.h line 7

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * of this software and associated documentation files (the

"Software"), to deal

include/hw/char/ibex_uart.h line 8

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * in the Software without restriction, including without limitation

the rights

include/hw/char/ibex_uart.h line 9

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or

sell

include/hw/char/ibex_uart.h line 13

Quoted 4 lines
include/hw/char/ibex_uart.h deleted +0 -4
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be

included in

include/hw/char/ibex_uart.h line 16

Quoted 3 lines
include/hw/char/ibex_uart.h deleted +0 -3
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS OR

include/hw/char/ibex_uart.h line 17

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

MERCHANTABILITY,

include/hw/char/ibex_uart.h line 18

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT

SHALL

include/hw/char/ibex_uart.h line 19

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

OTHER

include/hw/char/ibex_uart.h line 20

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

ARISING FROM,

include/hw/char/ibex_uart.h line 21

Quoted 1 line
include/hw/char/ibex_uart.h deleted +0 -1
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER

DEALINGS IN

Quoted 136 lines
include/hw/char/ibex_uart.h deleted +0 -52
- * THE SOFTWARE.
- */
-
-#ifndef HW_IBEX_UART_H
-#define HW_IBEX_UART_H
-
-#include "hw/core/sysbus.h"
-#include "chardev/char-fe.h"
-#include "qemu/timer.h"
-#include "qom/object.h"
-
-#define IBEX_UART_TX_FIFO_SIZE 16
-#define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
-
-#define TYPE_IBEX_UART "ibex-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
-
-struct IbexUartState {
-    /* <private> */
-    SysBusDevice parent_obj;
-
-    /* <public> */
-    MemoryRegion mmio;
-
-    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
-    uint32_t tx_level;
-
-    uint32_t rx_level;
-
-    QEMUTimer *fifo_trigger_handle;
-    uint64_t char_tx_time;
-
-    uint32_t uart_intr_state;
-    uint32_t uart_intr_enable;
-    uint32_t uart_ctrl;
-    uint32_t uart_status;
-    uint32_t uart_rdata;
-    uint32_t uart_fifo_ctrl;
-    uint32_t uart_fifo_status;
-    uint32_t uart_ovrd;
-    uint32_t uart_val;
-    uint32_t uart_timeout_ctrl;
-
-    Clock *f_clk;
-
-    CharFrontend chr;
-    qemu_irq tx_watermark;
-    qemu_irq rx_watermark;
-    qemu_irq tx_empty;
-    qemu_irq rx_overflow;
-};
-#endif /* HW_IBEX_UART_H */
include/hw/char/ot_uart.h new file +77 -0
new file mode 100644
index 0000000000..45ec3fc7c2
@@ -0,0 +1,133 @@
+/*
+ * QEMU OpenTitan UART device
+ *
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ *
+ * Author(s):
+ *  Loïc Lefort <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ */
+
+#ifndef HW_OPENTITAN_OT_UART_H
+#define HW_OPENTITAN_OT_UART_H
+
+#include "qemu/fifo8.h"
+#include "chardev/char-fe.h"
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+#include "hw/core/registerfields.h"
+
+#define TYPE_OT_UART "ot-uart"
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
+
+/* clang-format off */
+REG32(INTR_STATE, 0x00u)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0u, 1u)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1u, 1u)
+    SHARED_FIELD(INTR_TX_DONE, 2u, 1u)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3u, 1u)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4u, 1u)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5u, 1u)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6u, 1u)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7u, 1u)
+    SHARED_FIELD(INTR_TX_EMPTY, 8u, 1u)
+REG32(INTR_ENABLE, 0x04u)
+REG32(INTR_TEST, 0x08u)
+REG32(ALERT_TEST, 0x0cu)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u)
+REG32(CTRL, 0x10u)
+    FIELD(CTRL, TX, 0u, 1u)
+    FIELD(CTRL, RX, 1u, 1u)
+    FIELD(CTRL, NF, 2u, 1u)
+    FIELD(CTRL, SLPBK, 4u, 1u)
+    FIELD(CTRL, LLPBK, 5u, 1u)
+    FIELD(CTRL, PARITY_EN, 6u, 1u)
+    FIELD(CTRL, PARITY_ODD, 7u, 1u)
+    FIELD(CTRL, RXBLVL, 8u, 2u)
+    FIELD(CTRL, NCO, 16u, 16u)
+REG32(STATUS, 0x14u)
+    FIELD(STATUS, TXFULL, 0u, 1u)
+    FIELD(STATUS, RXFULL, 1u, 1u)
+    FIELD(STATUS, TXEMPTY, 2u, 1u)
+    FIELD(STATUS, TXIDLE, 3u, 1u)
+    FIELD(STATUS, RXIDLE, 4u, 1u)
+    FIELD(STATUS, RXEMPTY, 5u, 1u)
+REG32(RDATA, 0x18u)
+    FIELD(RDATA, RDATA, 0u, 8u)
+REG32(WDATA, 0x1cu)
+    FIELD(WDATA, WDATA, 0u, 8u)
+REG32(FIFO_CTRL, 0x20u)
+    FIELD(FIFO_CTRL, RXRST, 0u, 1u)
+    FIELD(FIFO_CTRL, TXRST, 1u, 1u)
+    FIELD(FIFO_CTRL, RXILVL, 2u, 3u)
+    FIELD(FIFO_CTRL, TXILVL, 5u, 3u)
+REG32(FIFO_STATUS, 0x24u)
+    FIELD(FIFO_STATUS, TXLVL, 0u, 8u)
+    FIELD(FIFO_STATUS, RXLVL, 16u, 8u)
+REG32(OVRD, 0x28u)
+    FIELD(OVRD, TXEN, 0u, 1u)
+    FIELD(OVRD, TXVAL, 1u, 1u)
+REG32(VAL, 0x2cu)
+    FIELD(VAL, RX, 0u, 16u)
+REG32(TIMEOUT_CTRL, 0x30u)
+    FIELD(TIMEOUT_CTRL, VAL, 0u, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31u, 1u)
+/* clang-format on */
Quoted 3 lines

These shouldn't need to be public

agreed. They are here because the struct definition below uses

REGS_COUNT which is computed based on these definitions. I can hard-code the length of 13 instead? It looks like other hardware blocks also hard-code this, is there some way to at least catch the error if this stops being self-consistent?

Yeah, everyone else just hard codes a length or defines each register as a u32/u64.

Good question about an assert. You could do a runtime assert in the init() function. The BSD user headers also use _Static_assert() [1], so that should be an option. I haven't checked that all supported compilers support _Static_assert though

1: https://en.cppreference.com/w/c/language/_Static_assert.html

Alistair

include/hw/char/ot_uart.h

Quoted 6 lines

Alistair

include/hw/char/ot_uart.h new file +3 -0
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK |

INTR_TX_DONE_MASK | \

Quoted 1 line

+ INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK |

INTR_RX_BREAK_ERR_MASK | \

Quoted 1 line

+ INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK |

INTR_TX_EMPTY_MASK)

include/hw/char/ot_uart.h

Quoted 3 lines
include/hw/char/ot_uart.h new file +3 -0
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK |

R_CTRL_SLPBK_MASK | \

Quoted 1 line

+ R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK

| \

include/hw/char/ot_uart.h

Quoted 4 lines
include/hw/char/ot_uart.h new file +4 -0
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define CTRL_SUP_MASK \
+    (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK |

R_CTRL_NCO_MASK)

include/hw/char/ot_uart.h

Quoted 13 lines
include/hw/char/ot_uart.h new file +13 -0
+
+#define OT_UART_NCO_BITS     16u
+#define OT_UART_TX_FIFO_SIZE 128u
+#define OT_UART_RX_FIFO_SIZE 128u
+#define OT_UART_IRQ_NUM      9u
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] :

"?")

Quoted 11 lines
include/hw/char/ot_uart.h new file +11 -0
+
+struct OtUARTState {
+    SysBusDevice parent_obj;
+    MemoryRegion mmio;
+    qemu_irq irqs[OT_UART_IRQ_NUM];
+
+    uint32_t regs[REGS_COUNT];
+
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+    uint32_t tx_watermark_level;

include/hw/char/ot_uart.h

Quoted 10 lines
include/hw/char/ot_uart.h new file +10 -0
+    bool in_break;
+    guint watch_tag;
+    unsigned pclk; /* Current input clock */
+    const char *clock_src_name; /* IRQ name once connected */
+
+    char *ot_id;
+    char *clock_name;
+    DeviceState *clock_src;
+    CharFrontend chr;
+    bool oversample_break; /* Should mock break in the oversampled VAL

reg? */

Quoted 34 lines
include/hw/char/ot_uart.h new file +9 -0
+    bool toggle_break; /* Are incoming breaks temporary or toggled? */
+};
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
+#endif /* HW_OPENTITAN_OT_UART_H */
include/hw/riscv/opentitan.h +2 -3
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
 #include "hw/timer/ibex_timer.h"
 #include "hw/ssi/ibex_spi_host.h"
 #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
     IbexTimerState timer;
     IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
--

2.49.1

Quoted 3 lines

--
Lex Bailey (they/them)
lowRISC C.I.C. 7 Hills Road, Cambridge, CB2 1GE, UK. Registered in

England & Wales 09272283 [email protected]

#6 lore raw
In reply to ~lexbaileylowrisc
Quoted 9 lines · ~lexbaileylowrisc

This commit is part of our efforts to get support for opentitan version 1 in qemu, it replaces the previous proposed change, and incorporates some requests from review comments.

The changes are now being made in the existing opentitan machine, instead of in a new machine. We have also avoided adding any new helper units.

Great! That's a good improvement.

Now when submitting patches you will want to try and have small self contained changes. So instead of deleting the current code and adding an entirely new file in one giant commit can you please make changes to the existing code one change at a time. I suspect the UART probably hasn't changed that much, so there shouldn't be too many commits to update it to support OT 1.0. That makes it easier to bisect issues if regressions are found and makes it a lot easier to review.

Quoted 9 lines

This updates the UART device to match our branch, and to match version 1 of opentitan.
This is not where we were going to start previously, but now this is the easiest starting point.

This commit adds a file that is a substantial copy of code already published under the MIT licence, and we aren't able to relicence it as GPL. (unable to get unanimous author agreement)

You might need a lawyer to review this. The new code says:

"""
* SPDX-License-Identifier: MIT
*
* Based on original ibex_uart implementation:
* Copyright (c) 2020 Western Digital
* Alistair Francis <[email protected]>
"""

So it's based on my original GPL code. So I don't think it can be relicensed as MIT. As such it should probably keep the original GPL license. I am now a lawyer though. This type of thing will need to be flagged in the commit message as well

Alistair

Quoted 21 lines

Lex Bailey (1):
Replace opentitan UART device with newer version (opentitan 1.0)

 hw/char/ibex_uart.c          | 569 -------------------------------
 hw/char/meson.build          |   2 +-
 hw/char/ot_uart.c            | 635 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   8 +
 hw/riscv/opentitan.c         |   3 +-
 include/hw/char/ibex_uart.h  |  73 ----
 include/hw/char/ot_uart.h    | 133 ++++++++
 include/hw/riscv/opentitan.h |   4 +-
 8 files changed, 781 insertions(+), 646 deletions(-)
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h

--
2.49.1

#7 lore raw
Quoted 2 lines · Alistair Francis

So it's based on my original GPL code. So I don't think it can be relicensed as MIT.

Hi Alistair,

Can you please clarify that?

My understanding is that QEMU, as a whole, is licensed as GPL but some individual files have different licenses. Looking at
https://github.com/qemu/qemu/blob/master/hw/char/ibex_uart.c it has the license text from the MIT license, which I interpret as being MIT-licensed. That was also the case when Loïc created ot_uart.c based on ibex_uart.c. Is there really a relicensing happening here? I thought we were just keeping the existing MIT license.

Luís

#8 lore raw
Quoted 7 lines · Luís Marques
Quoted 2 lines · Alistair Francis

So it's based on my original GPL code. So I don't think it can be relicensed as MIT.

Hi Alistair,

Quoted 7 lines

Can you please clarify that?

My understanding is that QEMU, as a whole, is licensed as GPL but some individual files have different licenses. Looking at
https://github.com/qemu/qemu/blob/master/hw/char/ibex_uart.c it has the license text from the MIT license, which I interpret as being MIT-licensed. That was also the case when Loïc created ot_uart.c based

Oh! Sorry about that, you are right.

I didn't include a SPDX-License-Identifier in the original code and when I skimmed the license I assumed it was GPL.

Quoted 2 lines

on ibex_uart.c. Is there really a relicensing happening here? I thought we were just keeping the existing MIT license.

Nope, my mistake.

Alistair

Quoted 2 lines

Luís